Spring Boot 报 required a bean of type … that could not be found,到底是哪里没配对?这篇用同一个报错的六种成因逐个复现,再测 debug=true 的条件报告和 TRACE 扫描日志各能区分哪几种。
[一] 忘了加 @Component:Foo 是普通类
说明:Parameter 0 of constructor in demo.User required a bean of type 'demo.Foo' that could not be found.
建议:Consider defining a bean of type 'demo.Foo' in your configuration.
[二] Foo 加了 @Component,但在主类所在包之外(demo.app 之外的 demo.other)
说明:Parameter 0 of constructor in demo.app.User required a bean of type 'demo.other.Foo' that could not be found.
建议:Consider defining a bean of type 'demo.other.Foo' in your configuration.
[三] Foo 带 @Profile("prod"),没有激活 prod
说明:Parameter 0 of constructor in demo.User required a bean of type 'demo.Foo' that could not be found.
建议:Consider defining a bean of type 'demo.Foo' in your configuration.
[四] Foo 带 @ConditionalOnProperty("feature.on"),属性没设
说明:Parameter 0 of constructor in demo.User required a bean of type 'demo.Foo' that could not be found.
建议:Consider defining a bean of type 'demo.Foo' in your configuration.
[五] 依赖的是接口,没有任何实现类成为 Bean
说明:Parameter 0 of constructor in demo.User required a bean of type 'demo.Foo' that could not be found.
建议:Consider defining a bean of type 'demo.Foo' in your configuration.
[六] @Bean 方法的返回类型写成了 Object
说明:Parameter 0 of constructor in demo.User required a bean of type 'demo.Foo' that could not be found.
建议:Consider defining a bean of type 'demo.Foo' in your configuration.
这六种原因里,把类名统一之后,「说明 + 建议」的不同写法一共 1 种六种成因的 FailureAnalyzer 说明与建议完全相同。
[一] debug=true 的 CONDITIONS EVALUATION REPORT(162 行)里提到 Foo:没有;TRACE 日志里 Scanning file Foo.class:True,Ignored because not matching any filter:True [二] debug=true 的 CONDITIONS EVALUATION REPORT(162 行)里提到 Foo:没有;TRACE 日志里 Scanning file Foo.class:False,Ignored because not matching any filter:False [三] debug=true 的 CONDITIONS EVALUATION REPORT(162 行)里提到 Foo:没有;TRACE 日志里 Scanning file Foo.class:True,Ignored because not matching any filter:True [四] debug=true 的 CONDITIONS EVALUATION REPORT(166 行)里提到 Foo:Foo: Did not match: - @ConditionalOnProperty (feature.on) did not find property 'feature.on' (OnPropertyCondition);TRACE 日志里 Scanning file Foo.class:True,Ignored because not matching any filter:True [五] debug=true 的 CONDITIONS EVALUATION REPORT(162 行)里提到 Foo:没有;TRACE 日志里 Scanning file Foo.class:True,Ignored because not matching any filter:True [六] debug=true 的 CONDITIONS EVALUATION REPORT(162 行)里提到 Foo:没有;TRACE 日志里 Scanning file Foo.class:True,Ignored because not matching any filter:True
条件报告只能抓条件不满足的;TRACE 扫描日志能区分「没扫到」与「被忽略」。
[七] @Qualifier 的名字写错
说明:Parameter 0 of constructor in demo.User required a bean of type 'demo.Foo' that could not be found.
建议:Consider revisiting the entries above or defining a bean of type 'demo.Foo' in your configuration.
[八] 泛型不匹配:有 List<String> 的 Bean,却要 List<Integer>
注入到的 List<Integer> = [],类型 ArrayList;启动成功
[九] 类在 classpath 上,但自动配置没有生效:只有 starter-web,要注入 RestClient.Builder
说明:Parameter 0 of constructor in demo.User required a bean of type 'org.springframework.web.client.RestClient$Builder' that could not be found.
建议:Consider defining a bean of type 'org.springframework.web.client.RestClient$Builder' in your configuration.
debug=true 的条件报告(297 行)里提到 RestClient:没有@Qualifier 写错、List 泛型不匹配(注入空集合)、缺自动配置模块。
说明:Parameter 0 of constructor in demo.User required a single bean, but 2 were found: - a: defined in file [<jtmp>/x155_十/out/demo/A.class] - b: defined in file [<jtmp>/x155_十/out/demo/B.class] 建议:Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
NoUniqueBeanDefinition 的报错列出候选并给出三条建议。
[十一] 异常 NoSuchBeanDefinitionException:No bean named 'fooo' available [十二] 三种写法的结果:Optional.isPresent=false,ObjectProvider.getIfAvailable=null,required=false 的字段=null
getBean 写错名字是运行时异常;可选依赖写法不会因没有 Bean 而失败。