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);
    	}
    }

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

  • 相关阅读:
    python 单例模式
    socketserver 多进程、多线程应用实例
    socket 编程的一些应用例子
    模拟一个http 请求的json格式报文,带 rsa 签名操作
    python excel基本操作
    多线程 进程间共享变量等
    多线程 multiprocessing 的几个小例子
    mysql 数据库的相关操作
    正则表达式匹配IP地址
    32-服务的容量规划:怎样才能做到有备无患
  • 原文地址:https://www.cnblogs.com/bedlimate/p/8660839.html
Copyright © 2011-2022 走看看