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

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

  • 相关阅读:
    Qt数据库集成应用封装
    Qt个人研究进展
    Qt仿win7自动顶部最大化左侧右侧半屏效果
    Qt编写QUI皮肤生成器
    java定时任务
    进程间通信(java)--队列
    单例设计模式-java
    Java RMI
    远程调用方式概述
    IO模型-java版
  • 原文地址:https://www.cnblogs.com/bedlimate/p/8660839.html
Copyright © 2011-2022 走看看