springBoot 依賴包中含有同樣的類名,注入報錯
阿新 • • 發佈:2019-01-29
場景:
1、 pom.xml 檔案中引入 sla jar 包中com.puhui.sla.OAuthServerConfig。
2、pom.xml 檔案中引入 cloud jar 包中com.puhui.cloud.config.OAuthServerConfig。
現象:
專案啟動報錯:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.puhui.RiskAssessApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'OAuthServerConfig' for bean class [com.puhui.sla.OAuthServerConfig] conflicts with existing, non-compatible bean definition of same name and class [com.puhui.cloud.config.OAuthServerConfig] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:183) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) at com.puhui.RiskAssessApplication.main(RiskAssessApplication.java:52) Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'OAuthServerConfig' for bean class [com.puhui.sla.OAuthServerConfig] conflicts with existing, non-compatible bean definition of same name and class [com.puhui.cloud.config.OAuthServerConfig] at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:345) at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:283) at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:135) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:289) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:247) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:200) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:169) ... 13 common frames omitted
解決方案:
在專案啟動主類中增加如下註解排除類。
@ComponentScan(excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = OAuthServerConfig.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = OAuthClientConfig.class) })