自动配置到底是怎么被加载的?为什么我的 @ConditionalOnBean 没生效?after 和 before 有什么用?没列在 imports 里会怎样?怎么看到每个条件为什么失败?这篇自己写了 5 个自动配置类,在 Spring Boot 4.0.8 上逐个验证。
afterMarker 存在
tooEarlyMarker 不存在
ghostMarker 不存在
unlistedMarker 不存在
userDependsOnGreeter 不存在
user2DependsOnGreeter 不存在
Greeter 类型的 Bean = [defaultGreeter],greet() = [default greeter(来自自动配置)]
--- 自己写的几个配置类在条件报告里的结果 ---
UserConfig#userDependsOnGreeter → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
UserConfig#userGreeter → 不匹配
✗ OnPropertyCondition: @ConditionalOnProperty (user.greeter) did not find property 'user.greeter'
UserConfig2 → 不匹配
✗ OnPropertyCondition: @ConditionalOnProperty (user.greeter) did not find property 'user.greeter'
lib.DecoratorAutoConfiguration → 匹配
✓ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) found bean 'defaultGreeter'
lib.GhostAutoConfiguration → 不匹配
✗ OnClassCondition: @ConditionalOnClass did not find required class 'com.nowhere.Missing'
lib.GreetingAutoConfiguration → 匹配
✓ OnClassCondition: @ConditionalOnClass found required class 'demo.lib.Greeter'
✓ OnPropertyCondition: @ConditionalOnProperty (greeting.enabled=true) matched
lib.GreetingAutoConfiguration#defaultGreeter → 匹配
✓ OnBeanCondition: @ConditionalOnMissingBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans
lib.TooEarlyAutoConfiguration → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
Boot 自带的 *AutoConfiguration 在条件报告里出现 24 个,其中条件全部满足(生效)的 5 个;spring.autoconfigure.exclude 排除掉的 = []
Bean 总数 = 77imports 里列的才导入;after / before 决定 @ConditionalOnBean 能否看到对方的 Bean。
afterMarker 不存在
tooEarlyMarker 不存在
ghostMarker 不存在
unlistedMarker 不存在
userDependsOnGreeter 不存在
user2DependsOnGreeter 不存在
Greeter 类型的 Bean = [],greet() = []
--- 自己写的几个配置类在条件报告里的结果 ---
UserConfig#userDependsOnGreeter → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
UserConfig#userGreeter → 不匹配
✗ OnPropertyCondition: @ConditionalOnProperty (user.greeter) did not find property 'user.greeter'
UserConfig2 → 不匹配
✗ OnPropertyCondition: @ConditionalOnProperty (user.greeter) did not find property 'user.greeter'
lib.DecoratorAutoConfiguration → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
lib.GhostAutoConfiguration → 不匹配
✗ OnClassCondition: @ConditionalOnClass did not find required class 'com.nowhere.Missing'
lib.GreetingAutoConfiguration → 不匹配
✓ OnClassCondition: @ConditionalOnClass found required class 'demo.lib.Greeter'
✗ OnPropertyCondition: @ConditionalOnProperty (greeting.enabled=true) found different value in property 'enabled'
lib.TooEarlyAutoConfiguration → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
Boot 自带的 *AutoConfiguration 在条件报告里出现 24 个,其中条件全部满足(生效)的 5 个;spring.autoconfigure.exclude 排除掉的 = []
Bean 总数 = 73一个条件失败连带依赖它的配置。
afterMarker 存在
tooEarlyMarker 存在
ghostMarker 不存在
unlistedMarker 不存在
userDependsOnGreeter 不存在
user2DependsOnGreeter 存在
Greeter 类型的 Bean = [userGreeter],greet() = [user greeter(用户自己定义)]
--- 自己写的几个配置类在条件报告里的结果 ---
UserConfig#userDependsOnGreeter → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
UserConfig#userGreeter → 匹配
✓ OnPropertyCondition: @ConditionalOnProperty (user.greeter) matched
UserConfig2 → 匹配
✓ OnPropertyCondition: @ConditionalOnProperty (user.greeter) matched
UserConfig2#user2DependsOnGreeter → 匹配
✓ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) found bean 'userGreeter'
lib.DecoratorAutoConfiguration → 匹配
✓ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) found bean 'userGreeter'
lib.GhostAutoConfiguration → 不匹配
✗ OnClassCondition: @ConditionalOnClass did not find required class 'com.nowhere.Missing'
lib.GreetingAutoConfiguration → 匹配
✓ OnClassCondition: @ConditionalOnClass found required class 'demo.lib.Greeter'
✓ OnPropertyCondition: @ConditionalOnProperty (greeting.enabled=true) matched
lib.GreetingAutoConfiguration#defaultGreeter → 不匹配
✗ OnBeanCondition: @ConditionalOnMissingBean (types: demo.lib.Greeter; SearchStrategy: all) found beans of type 'demo.lib.Greeter' userGreeter
lib.TooEarlyAutoConfiguration → 匹配
✓ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) found bean 'userGreeter'
Boot 自带的 *AutoConfiguration 在条件报告里出现 24 个,其中条件全部满足(生效)的 5 个;spring.autoconfigure.exclude 排除掉的 = []
Bean 总数 = 81@ConditionalOnMissingBean 让路;用户 Bean 在先也改变了别的配置的评估结果。
afterMarker 不存在
tooEarlyMarker 不存在
ghostMarker 不存在
unlistedMarker 不存在
userDependsOnGreeter 不存在
user2DependsOnGreeter 不存在
Greeter 类型的 Bean = [],greet() = []
--- 自己写的几个配置类在条件报告里的结果 ---
UserConfig#userDependsOnGreeter → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
UserConfig#userGreeter → 不匹配
✗ OnPropertyCondition: @ConditionalOnProperty (user.greeter) did not find property 'user.greeter'
UserConfig2 → 不匹配
✗ OnPropertyCondition: @ConditionalOnProperty (user.greeter) did not find property 'user.greeter'
lib.DecoratorAutoConfiguration → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
lib.GhostAutoConfiguration → 不匹配
✗ OnClassCondition: @ConditionalOnClass did not find required class 'com.nowhere.Missing'
lib.TooEarlyAutoConfiguration → 不匹配
✗ OnBeanCondition: @ConditionalOnBean (types: demo.lib.Greeter; SearchStrategy: all) did not find any beans of type demo.lib.Greeter
Boot 自带的 *AutoConfiguration 在条件报告里出现 24 个,其中条件全部满足(生效)的 5 个;spring.autoconfigure.exclude 排除掉的 = [demo.lib.GreetingAutoConfiguration]
Bean 总数 = 73spring.autoconfigure.exclude 按类名在评估前排除。