I have a Spring project where multiple beans may have the same bean name.
In order to avoid ConflictingBeanDefinitionException
the project has an overridden ContextNamespaceHandler
.
public class ContextNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser() {
@Override
protected ClassPathBeanDefinitionScanner createScanner(XmlReaderContext readerContext, boolean useDefaultFilters) {
return new ClassPathBeanDefinitionScanner(readerContext.getRegistry(), useDefaultFilters) {
@Override
protected boolean checkCandidate(String beanName, BeanDefinition beanDefinition) throws IllegalStateException {
return true;
}
};
}
});
}
}
I'm using Swagger/Springfox to generate API documentation for the project.
@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = { "some.package", "some.other.package" })
public class SwaggerConfig {
// ...
}
@ComponentScan
is causing ConflictingBeanDefinitionException
as it is using default ClassPathBeanDefinitionScanner
instead of the overridden one.
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'xxx' for bean class [xxx] conflicts with existing, non-compatible bean definition of same name and class [xxx]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:320)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:259)
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:140)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:262)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:226)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:193)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:163)
... 22 more
Is there a way to override ClassPathBeanDefinitionScanner
used by @ComponentScan
or other way to suppress ConflictingBeanDefinitionException
?