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

這樣,問題就可以解決了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM