简体   繁体   中英

How to create @Controller and @Service instance conditionally on Spring

I am using Spring Boot 1.5.9.

Is there a way to turn on/off @Controller and @Services ?

Something such as @ConditionalOnProperty , @Conditional for beans.

@ConditionalController // <--- something like this
@RestController
public class PingController {

    @Value("${version}")
    private String version;

    @RequestMapping(value = CoreHttpPathStore.PING, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Map<String, Object>> ping() throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("message", "Welcome to our API");
        map.put("date", new Date());
        map.put("version", version);
        map.put("status", HttpStatus.OK);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

}

Then use some configuration bean to load it up.

@ConditionalOnProperty should work for Controller (or Service) as well, since it is also a Spring bean.

Add to your PingController

@ConditionalOnProperty(prefix="ping.controller",
        name="enabled",
        havingValue="true")
@RestController
public class PingController {...}

and to the application.properties to turn it on/off

ping.controller.enabled=false

do you want to try and load the bean programmatically instead?

you could access the application context using one of the two mechanisms

@Autowired private ApplicationContext appContext;

or creating something like a bean factory by extending ApplicationAware

public class ApplicationContextProvider implements ApplicationContextAware{

once you have a handle to the application context you can add a bean to the context programmatically.

By default in Spring, all the defined beans, and their dependencies, are created when the application context is created.

We can turn it off by configuring a bean with lazy initialization, the bean will only be created, and its dependencies injected, once they're needed.

You can enable lazy initialization by configuring application.properties .

spring.main.lazy-initialization=true

Setting the property value to true means that all the beans in the application will use lazy initialization.

All the defined beans will use lazy initialization, except for those that we explicitly configure with @Lazy(false) .

Or you can do it through the @Lazy approach. When we put @Lazy annotation over the @Configuration class, it indicates that all the methods with @Bean annotation should be loaded lazily.

@Lazy
@Configuration
@ComponentScan(basePackages = "com.app.lazy")
public class AppConfig {

    @Bean
    public Region getRegion(){
        return new Region();
    }

    @Bean
    public Country getCountry(){
        return new Country();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM