【转载】Springboot 2.3.5 控制器重名导致应用启动失败的问题解决
参考
原因
- spring提供两种beanName生成策略,基于注解的sprong-boot默认使用的是AnnotationBeanNameGenerator,它生成beanName的策略就是,取当前类名(不是全限定类名)作为beanName。由此,如果出现不同包结构下同样的类名称,肯定会出现冲突。
- 解决方法
1.
- 自己写一个类实现 org.springframework.beans.factory.support.BeanNameGeneraot接口
- 在启动类上加注解@ComponentScan(nameGenerator = UniqueNameGenerator.class)使刚才我们自定义的BeanName生成策略生效。
2.
- 将其中一个实现类改为不同的名字
步骤
- config/创建 UniqueNameGenerator.java
public class UniqueNameGenerator extends AnnotationBeanNameGenerator {
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
//全限定类名
String beanName = definition.getBeanClassName();
return beanName;
}
}
- 入口文件 Application 添加
/**
* 控制器重名导致启动失败
*/
@ComponentScan(nameGenerator = UniqueNameGenerator.class)
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/articles/15149471.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/articles/15149471.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。

浙公网安备 33010602011771号