背景:本项目引入了一个三方jar包,但jar中出现了一个类名相同,但包名不同,导致spring启动失败 解决方案:
方案一:排查下在本项目调用过程中是否不会用到某一个深层jar,要是没用到,则在引入该jar的坐标时用以下方式排除掉该冲突类所在的深层jar
<dependency>
<artifactId>三方jar</artifactId>
<groupId>三方jar</groupId>
<version>三方jar</version>
<exclusions>
<exclusion>
<artifactId>冲突包所在的深层jar</artifactId>
<groupId>冲突包所在的深层jar</groupId>
</exclusion>
</exclusions>
</dependency>
方案二:排出下本项目在调用过程中是否不会用到某一个冲突的类(两个都用不到更好,不过只为了解决冲突,则只需处理一个即可),要是没用到,则在spring扫描该jar包时,将该类忽略掉不加载即可。这种方式比第一个更精确
<context:component-scan base-package="com.*" >
<context:exclude-filter type="annotation" expression="冲突类全名(包含包路径啊)"/>
</context:component-scan>
方案三:转载其他地方的(原地址:https://www.cnblogs.com/bedlimate/p/8660839.html)
项目背景:
某日,有需求要在三天的时间内完成两个大项目的项目合并,因为之前两个项目的包结构和类名都很多相同,于是开始考虑使用加一级包进行隔离,类似于这种结构

但是在启动的过程中,抛出来这样的异常:
1 2 3 4 5 6 7 8 9 | Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'nameConflict' for bean class [xom.liuyun.beannameconflict.modelB.NameConflict] conflicts with existing, non-compatible bean definition of same name and class [xom.liuyun.beannameconflict.modelA.NameConflict]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:348) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:286) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:284) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:241) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:166) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 13 common frames omitted。
|
原因:
spring提供两种beanName生成策略,基于注解的sprong-boot默认使用的是AnnotationBeanNameGenerator,它生成beanName的策略就是,取当前类名(不是全限定类名)作为beanName。由此,如果出现不同包结构下同样的类名称,肯定会出现冲突。
解决方案如下:
1. 自己写一个类实现 org.springframework.beans.factory.support.BeanNameGeneraot接口
1 2 3 4 5 6 7 8 9 | public class UniqueNameGenerator extends AnnotationBeanNameGenerator {
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
//全限定类名
String beanName = definition.getBeanClassName();
return beanName;
}
}
|
2. 在启动类上加注解@ComponentScan(nameGenerator = UniqueNameGenerator.class)使刚才我们自定义的BeanName生成策略生效。
1 2 3 4 5 6 7 8 | @SpringBootApplication
@ComponentScan(nameGenerator = UniqueNameGenerator.class)
public class BeanNameConflictApplication {
public static void main(String[] args) {
SpringApplication.run(BeanNameConflictApplication.class, args);
}
}
|
这样,问题就可以解决了。