我开发了一个自定义Spring Boot自动配置,以便于使用专有的消息传递库。
主要的自动配置类基本如下:
@Configuration
@ConditionalOnClass({LibServer.class, LibClient.class})
@EnableConfigurationProperties(LibProperties.class)
public class LibAutoConfiguration {
@Autowired
LibProperties props;
@Bean
@ConditionalOnMissingBean(LibServer.class)
public LibServer lbServ() {
// create and configure a server object
}
@Bean
@ConditionalOnMissingBean(LibClient.class)
public LibClient lbClient() {
//create and configure a client object
}
}
然而,条件注释似乎没有检测到主@SpringBootApplication
注释类中声明的bean。
它只检测在单独的@Configuration
注释类中声明的bean。
也就是说,如果我放置两个@Bean
带注释的方法,在主类中返回LibServer
和LibClient
个对象,我最终得到两个LibServer
和两个{ {1}}上下文中的对象(自动配置的和明确声明的对象)。
本机spring引导自动配置(例如LibClient
one)也可以检测主类中声明的bean(例如DataSource
带注释的@Bean
方法)。
即使是在主类中声明的bean,我如何获得正确的bean检测?
修改
展示行为的完整多模块maven项目位于https://github.com/AlexFalappa/spring-boot-testcase
答案 0 :(得分:1)
如果在application.properties
(logging.level.org.springframework=DEBUG
)中设置调试日志级别,您会注意到Spring将检测这两个定义。但是你也会看到the order in which this happens may not be what you expected,因为它首先从库配置实例化bean,然后从主类实例化AFTERWARDS,因此你得到2个实例(剥离时间戳使其更友好) :
Bean定义
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'autoConfigurationReport'
a.ConfigurationClassBeanDefinitionReader : Registering bean definition for @Bean method af.spring.boot.libbo.LibAutoConfiguration.lbServ()
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.boot.autoconfigure.condition.BeanTypeRegistry'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'autoConfigurationReport'
a.ConfigurationClassBeanDefinitionReader : Registering bean definition for @Bean method af.spring.boot.libbo.LibAutoConfiguration.lbClient()
a.ConfigurationClassBeanDefinitionReader : Registering bean definition for @Bean method af.DemoLibboApplication.libServ()
a.ConfigurationClassBeanDefinitionReader : Registering bean definition for @Bean method af.DemoLibboApplication.libClient()
Bean实例化
o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'lbServ'
o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'lbServ'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'libAutoConfiguration'
Autoconfiguring LibServer
o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'lbServ' to allow for resolving potential circular references
o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'lbServ'
o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'lbClient'
o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'lbClient'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'libAutoConfiguration'
Autoconfiguring LibClient
o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'lbClient' to allow for resolving potential circular references
o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'lbClient'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'lib.CONFIGURATION_PROPERTIES'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.store'
o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'libServ'
o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'libServ'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'demoLibboApplication'
o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'libServ' to allow for resolving potential circular references
o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'libServ'
o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'libClient'
o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'libClient'
o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'demoLibboApplication'
o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'libClient' to allow for resolving potential circular references
o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'libClient'
您还可以在AUTO-CONFIGURATION REPORT
中查看当前实现中评估LibAutoConfiguration
中的条件时,它们是否匹配,通常会创建bean:
Positive matches:
-----------------
...
LibAutoConfiguration#lbClient matched
- @ConditionalOnMissingBean (types: af.libbo.LibClient; SearchStrategy: all) found no beans (OnBeanCondition)
LibAutoConfiguration#lbServ matched
- @ConditionalOnMissingBean (types: af.libbo.LibServer; SearchStrategy: all) found no beans (OnBeanCondition)
...
但是,如果您向主类添加相同的条件,您将看到它将根据LibAutoConfiguration
中的定义创建bean,并在尝试时为DemoLibboApplication
创建它们,它实际上会找到以前创建的bean并跳过实例化:
Negative matches:
-----------------
...
DemoLibboApplication#libClient did not match
- @ConditionalOnMissingBean (types: af.libbo.LibServer; SearchStrategy: all) found the following [lbServ] (OnBeanCondition)
DemoLibboApplication#libServ did not match
- @ConditionalOnMissingBean (types: af.libbo.LibServer; SearchStrategy: all) found the following [lbServ] (OnBeanCondition)
...
答案 1 :(得分:1)
您自己没有导入LibAutoConfiguration,是吗?
这是提示:您的主类位于自动配置类的父包中。因此,您实际上是通过组件扫描实际导入@Configuration
。事实证明,当您处理该类时(通过显式导入而不是通过自动配置),尚未创建任何bean,因此它确实创建了它们。稍后处理您的应用程序类并创建这些bean。
如果您将定义移到其他位置,可能可以正常工作(因为您已经用LibConfig
计算了自己),但这不是确定性的。
TL;DR
确保您的自动配置代码位于单独的空间中,并且不是组件扫描的目标。我已将您的DemoLibboApplication
移至demo
包,并且按预期工作。