bigywater.tech
Spring · 155

NoSuchBeanDefinitionException:几种成因逐个复现

Spring Boot 报 required a bean of type … that could not be found,到底是哪里没配对?这篇用同一个报错的六种成因逐个复现,再测 debug=true 的条件报告和 TRACE 扫描日志各能区分哪几种。

1 种写法
六种不同成因触发的启动报错,把类名统一之后「说明加建议」的不同写法个数
1 / 6
debug=true 的条件报告里提到 Foo 的场景数(只有 @ConditionalOnProperty 那一种)
长度为 0
有 List 的 Bean 却要 List 时,注入进来的集合是一个空列表,不报错

同一个报错

实测 · 本机执行输出
  [一] 忘了加 @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 而失败。

分步演示

排错
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / OpenJDK 21.0.12(Homebrew) / Spring Boot 4.0.8(Spring Framework 7.0.9、Tomcat 11.0.24) / Spring Cloud 2025.1.3;用 javac 编译、java 直接运行,依赖由 Maven 从 Maven Central 解析;数字每次运行会有小幅抖动(Spring Boot 4.0.8 的 spring-boot-starter;每个场景是一个独立的最小应用,直接 SpringApplication.run,只保留 FailureAnalyzer 的日志输出,再用 debug=true 与 TRACE 日志各重跑一次)
    • 没有验证:Web 应用里 Controller、Filter 的注入失败;循环依赖导致的报错(见专门一篇);@Lazy、@DependsOn 的影响;Kotlin、Groovy 写法;GraalVM Native 下的差异;@ComponentScan 自定义 basePackages 或 excludeFilters 的写法;IDE 里的提示
    京ICP备2026067502号-1京公网安备11010502063487号