zoukankan      html  css  js  c++  java
  • 谈谈spring-boot不同包结构下,同样的类名冲突导致服务启动失败解决方案

    项目背景:

      某日,有需求要在三天的时间内完成两个大项目的项目合并,因为之前两个项目的包结构和类名都很多相同,于是开始考虑使用加一级包进行隔离,类似于这种结构

    但是在启动的过程中,抛出来这样的异常:

    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接口

    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生成策略生效。 

    @SpringBootApplication
    @ComponentScan(nameGenerator = UniqueNameGenerator.class)
    public class BeanNameConflictApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(BeanNameConflictApplication.class, args);
    	}
    }

    这样,问题就可以解决了。

  • 相关阅读:
    .Net自动生成Html新闻系统V1.0 Beta 下载
    Visual Studio .NET 2003中自己找到的一个小技巧[图]
    多表连接的SQL写法(SqlServer、Oracle)
    在线人数统计 V1.0(Asp.net+ SqlServer) 源码下载
    Visual Studio 2005安装后,原来的Asp.net1.1不能执行的解决方法。
    [函数]截取固定长的字符串(双字节的计2位)
    [原创]asp.net 2.0下的自定义树(myTreeView)
    通用的数据库操作助手类
    关于时间国际化的方案
    HTTPS Cipher Suite问题
  • 原文地址:https://www.cnblogs.com/bedlimate/p/8660839.html
Copyright © 2011-2022 走看看