Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

java - Spring boot ConditionalOnBean annotation

There are spring boot 2.0.2 configuration

@Configuration
public class ApiConfig {

    @Bean
    @Profile("!tests")
    @ConditionalOnProperty(name = "enabled", havingValue = "true")
    public MyService service() {
        return new MyServiceImpl();
    }

}

... and some controller which should be created and added to application context only if MyService bean is initialized.

@RestController
@ConditionalOnBean(MyService.class)
public class MyController {
   @Autowired
   private MyService service;
}

It works Ok. But occasionally spring boot skips MyController creating. According to the logs MyService is created, but after any other beans(including all controllers), at the end.

Why boot does not process @Configuration beans prior to @RestController? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Why boot does not process @Configuration beans prior to @Controller? Thanks.

Because Spring doesn't guarantee that.
As well as @ConditionalOnBean warns about this kind of issue in this specification :

The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.

And you don't use the annotation in an auto-configuration class. You indeed specified it in a class annotated with @RestController.

I think that to achieve your requirement you should move the @RestController bean declaration in a @Configuration class that's imported via an EnableAutoConfiguration entry in spring.factories.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...