核心问题:在Spring Boot项目中,同名Bean的冲突可能导致ConflictingBeanDefinitionException异常。
解决策略:
-
更换类名:
- 当两个类未手动设置Bean名称时,修改其中一个类名以避免冲突。
-
手动设置Bean的名称:
- 使用
@Bean("bean1")注解来指定Bean名称,避免自动配置的Bean名称冲突。
- 使用
-
使用@Primary注解:
- 指定当存在多个同类型Bean时,哪个Bean应被优先考虑。
- 示例代码:
@Service @Primary public class CustomAuthCodeServiceImpl implements AuthCodeService { @Ov

最低0.47元/天 解锁文章
7646

被折叠的 条评论
为什么被折叠?



