ConflictingBeanDefinitionException: Annotation-specified bean name ‘XXX‘ for bean class

error message

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'mybatisInterceptor' for bean class [com.sinandata.service.commons.config.MybatisInterceptor] conflicts with existing, non-compatible bean definition of same name and class [com.sinandata.kg.report.config.MybatisInterceptor]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287)
	at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132)
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296)
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175)

This error is caused by having two bean definitions with the same name but incompatible classes in the Spring context. In your project, two XXX classes are defined and both use the same bean name XXX, which causes a conflict.

If the two beans are defined by yourself, you can solve them in the following ways:

  1. Modify bean name - Change the bean name of one of the MybatisInterceptors to a different name to avoid name conflicts.

  2. Distinguish class name or package name - distinguish the class name or package name of the two MybatisInterceptor classes to avoid class name conflicts.

  3. Use @Qualifier annotation - During bean injection, use @Qualifier annotation to specify the specific bean to be injected instead of using the default name. For example: @Autowired @Qualifier(“mybatisInterceptor1”) private MybatisInterceptor mybatisInterceptor;

If one of them is a dependent bean that comes with the external Jar package, then exclude the bean by
adding annotations to the startup class
@ComponentScan( excludeFilters = [ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = [MybatisInterceptor.class()])])

Explanation:
 In the Spring framework, the @ComponentScan annotation is used to search for classes marked with @Component, @Service, @Controller, @Repository, etc., and register them as beans in the Spring container. Sometimes, you may want to exclude certain classes, such as classes in third-party libraries or classes that come with the framework, to avoid conflicts.

 The excludeFilters attribute is an element of the @ComponentScan annotation that can be used to specify classes to be excluded. Among them, Filter is an enumerated type that contains four filter types: ANNOTATION, ASSIGNABLE_TYPE, ASPECTJ, and CUSTOM.

 In the provided code, the excludeFilters property uses a filter of type ASSIGNABLE_TYPE and specifies a class named MybatisInterceptor. This means that if a class is of the same type as MybatisInterceptor, it will be excluded from the scan and will not be registered as a bean in the Spring container.

Guess you like

Origin blog.csdn.net/java_cpp_/article/details/130968149