异常堆栈
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [io.renren.ApiApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'userService' for bean class [io.renren.service.impl.UserServiceImpl2] conflicts with existing, non-compatible bean definition of same name and class [io.renren.service.impl.UserServiceImpl]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
at io.renren.ApiApplication.main(ApiApplication.java:18)
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'userService' for bean class [io.renren.service.impl.UserServiceImpl2] conflicts with existing, non-compatible bean definition of same name and class [io.renren.service.impl.UserServiceImpl]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:348)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:286)
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:287)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167)
... 13 common frames omitted
复现
声明Service
复现:声明两个相同的value,然后运行
流程追踪
doScan
org.springframework.context.annotation.ClassPathBeanDefinitionScanner
AnnotationAttributes 获取
org.springframework.context.annotation.AnnotationBeanNameGenerator
并发Map保证唯一性
org.springframework.beans.factory.support.DefaultListableBeanFactory
/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap
= new ConcurrentHashMap<>(256);
默认添加Class类型的Value
org.springframework.util.ClassUtils
结束:
Application.run后会scan 编译目录(target)下的对应包名(packageName)下的所有class文件,通过正则匹配到相应的ClassName,用并发hash表管理起来。而解析和对比的规则是通过AnnotationConfigUtils获取的Value值作为Key值对比。如果 两个BeanDefinition的key相同,但是全路径(resource)却不同,就会抛出
conflicts with existing, non-compatible bean definition of same name
,也就是Service同名异常