我有一个Spring Boot 1.5.x项目,其中一些@Component依赖于其他@Component,并且最终沿着依赖链,可以使用@ConditionalOnProperty完全启用或禁用某些@Component.
我正在使用@ConditionalOnBean来避免实例化依赖于因缺少属性而未实例化的其他@Component的@Component.
但是,它只适用于直接依赖,而不适用于传递依赖,但我无法理解为什么.
让我试着用一个简单的例子来解释.
考虑MyServices.kt:
private val logger = KotlinLogging.logger {}
class MyServices
@ConditionalOnProperty("service.a")
@Service
class ServiceA {
init {
logger.info { "A SERVICE" }
}
}
@ConditionalOnBean(ServiceA::class)
@ConditionalOnProperty("service.b")
@Service
class ServiceB(
private val serviceA: ServiceA
) {
init {
logger.info { "B SERVICE depends on $serviceA" }
}
}
@ConditionalOnBean(ServiceB::class)
@ConditionalOnProperty("service.c")
@Service
class ServiceC(
private val serviceB: ServiceB
) {
init {
logger.info { "C Service depends on $serviceB" }
}
}
使用以下application.yml:
service:
a: false
b: true
c: true
然后Spring在启动时崩溃,具体如下:
**************************
APPLICATION Failed TO START
***************************
Description:
Parameter 0 of constructor in org.gotson.transitivebeandependencies.ServiceC required a bean of type 'org.gotson.transitivebeandependencies.ServiceB' that could not be found.
Action:
Consider defining a bean of type 'org.gotson.transitivebeandependencies.ServiceB' in your configuration.
以下是自动配置的结果:
Positive matches:
ServiceC matched:
- @ConditionalOnProperty (service.c) matched (OnPropertyCondition)
- @ConditionalOnBean (types: org.gotson.transitivebeandependencies.ServiceB; SearchStrategy: all) found bean 'serviceB' (OnBeanCondition)
Negative matches:
ServiceA:
Did not match:
- @ConditionalOnProperty (service.a) found different value in property 'service.a' (OnPropertyCondition)
ServiceB:
Did not match:
- @ConditionalOnBean (types: org.gotson.transitivebeandependencies.ServiceA; SearchStrategy: all) did not find any beans (OnBeanCondition)
Matched:
- @ConditionalOnProperty (service.b) matched (OnPropertyCondition)
但是,使用以下application.yml:
service:
a: true
b: false
c: true
然后一切正常,只有一个ServiceA实例被实例化,而没有创建ServiceB和ServiceC bean.
与@Bean而不是@Component相同的行为按预期工作.
MyBeans.kt:
private val logger = KotlinLogging.logger {}
@Configuration
class MyBeans {
@ConditionalOnProperty("bean.a")
@Bean
fun beanA(): BeanA {
logger.info { "A BEAN" }
return BeanA("beanA")
}
@ConditionalOnBean(BeanA::class)
@ConditionalOnProperty("bean.b")
@Bean
fun beanB(beanA: BeanA): BeanB {
logger.info { "B BEAN depends on $beanA" }
return BeanB("beanB")
}
@ConditionalOnBean(BeanB::class)
@ConditionalOnProperty("bean.c")
@Bean
fun beanC(beanB: BeanB): BeanC {
logger.info { "C BEAN depends on $beanB" }
return BeanC("beanC")
}
}
data class BeanA(val name: String)
data class BeanB(val name: String)
data class BeanC(val name: String)
使用application.yml:
bean:
a: false
b: true
c: true
我没有实例化BeanA,BeanB或BeanC类型的bean.
以下是自动配置的结果:
Negative matches:
MyBeans#beanA:
Did not match:
- @ConditionalOnProperty (bean.a) found different value in property 'bean.a' (OnPropertyCondition)
MyBeans#beanB:
Did not match:
- @ConditionalOnBean (types: org.gotson.transitivebeandependencies.BeanA; SearchStrategy: all) did not find any beans (OnBeanCondition)
Matched:
- @ConditionalOnProperty (bean.b) matched (OnPropertyCondition)
MyBeans#beanC:
Did not match:
- @ConditionalOnBean (types: org.gotson.transitivebeandependencies.BeanB; SearchStrategy: all) did not find any beans (OnBeanCondition)
Matched:
- @ConditionalOnProperty (bean.c) matched (OnPropertyCondition)
我已经设置了一个带有测试的样本repo来重现:https://github.com/gotson/spring-transitive
无论如何,订购是关键.如果希望bean类型匹配正常工作,则必须按给定顺序处理配置类.如果你有一个C1配置类,只有当bean B可用并且所述bean由C2提供时才提供bean A,C2必须先运行.
Spring Boot有两个解析阶段:首先我们解析所有用户的配置.完成后,我们解析自动配置的bean定义.自动配置本身是有序的(使用@AutoConfigureBefore,@ AutoConfigureAfter).这样,我们可以保证,如果您将@ConditionalOnBean置于自动配置中,它将按照预期处理用户配置.如果您依赖其他自动配置所贡献的内容,您可以使用这些注释轻松订购.
您的设置完全避免订购,因此如果订购是正确的,它可以工作,如果不是,则不工作. @ConditionalOnBean的Javadoc显然是states that
it is strongly recommended to use this condition on auto-configuration classes only.
如果您想了解更多信息,可以参加3小时的大学课程,即这个主题on youtube.