NoUniqueBeanDefinitionException 怎么修?字段名、@Primary、@Qualifier 各起什么作用,两个 @Primary 会怎样,@Qualifier 拼错报什么,ObjectProvider 的几个方法有什么差别?这篇用同一对 Bean 在 Spring Boot 4.0.8 上逐个试。
启动失败:NoUniqueBeanDefinitionException
报告:Field g in demo.c1.Consumer required a single bean, but 2 were found:
报告:- englishGreeter: defined by method 'englishGreeter' in demo.c1.Cfg
报告:- chineseGreeter: defined by method 'chineseGreeter' in demo.c1.Cfg
报告:This may be due to missing parameter name information
报告:Action:
报告: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
报告:Ensure that your compiler is configured to use the '-parameters' flag.
报告:You may need to update both your build tool settings as well as your IDE.
报告:(See https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.1-Release-Notes#parameter-name-retention)
报告:RES 启动失败:NoUniqueBeanDefinitionException字段名 g、两个候选,启动失败。
注入成功:hello 启动成功
字段名与 Bean 名相同就注入成功。
注入成功:你好 启动成功
标在 chineseGreeter 上。
注入成功:你好 启动成功
值是 Bean 名。
启动失败:NoUniqueBeanDefinitionException
没有 FailureAnalyzer 报告,日志里是完整堆栈;最后一个 Caused by:org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'demo.c5.Greeter' available: more than one 'primary' bean found among candidates: [englishGreeter, chineseGreeter]more than one primary。
启动失败:NoSuchBeanDefinitionException
报告:Field g in demo.c6.Consumer required a bean of type 'demo.c6.Greeter' that could not be found.
报告:The injection point has the following annotations:
报告:- @org.springframework.beans.factory.annotation.Autowired(required=true)
报告:- @org.springframework.beans.factory.annotation.Qualifier("chinseGreeter")
报告:The following candidates were found but could not be injected:
报告:- User-defined bean method 'englishGreeter' in 'Cfg'
报告:- User-defined bean method 'chineseGreeter' in 'Cfg'
报告:Action:
报告:Consider revisiting the entries above or defining a bean of type 'demo.c6.Greeter' in your configuration.
报告:RES 启动失败:NoSuchBeanDefinitionException报 NoSuchBeanDefinitionException。
List 里依次是:[你好, hello];Map 的键:[englishGreeter, chineseGreeter] 启动成功
不会触发异常,List 按 @Order。
getIfUnique() = null stream().count() = 2 getIfAvailable() 抛 NoUniqueBeanDefinitionException getObject() 抛 NoUniqueBeanDefinitionException 启动成功
getIfUnique 静默返回 null。
注入成功:hello 启动成功
带 -parameters 能对上。
class 文件里是否带 MethodParameters 属性:否(对照:加 -parameters 编译的那份:是)
启动失败:NoUniqueBeanDefinitionException
报告:Parameter 0 of constructor in demo.c9.Consumer required a single bean, but 2 were found:
报告:- englishGreeter: defined by method 'englishGreeter' in demo.c9.Cfg
报告:- chineseGreeter: defined by method 'chineseGreeter' in demo.c9.Cfg
报告:This may be due to missing parameter name information
报告:Action:
报告: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
报告:Ensure that your compiler is configured to use the '-parameters' flag.
报告:You may need to update both your build tool settings as well as your IDE.
报告:(See https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.1-Release-Notes#parameter-name-retention)
报告:RES 启动失败:NoUniqueBeanDefinitionException对不上,启动失败。