Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException Annotation error resolution

Title # Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException Annotation error resolution

springboot project startup error

foreword

The following is the processing process of this error report. If your project is also reported to have duplicate class names at startup, I hope my processing method will be useful to you.
The error message is as follows

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'shiroConfig' for bean class [cn.com.compass.web.hanlder.BaseExceptionHandle] conflicts with existing, non-compatible bean definition of same name and class [cn.com.compass.core.hanlder.BaseExceptionHandle]

Reason: I use an open source project: compass in my project. My project dependency tree tells me that project A depends on project B, and project B depends on compass. This error is reported after the project is packaged and deployed through Jenkins. In fact, core is not used in the project this package.
First of all, the first thing I thought of was that I added the dependency of excluding compass to the dependency of project B in the pom file, but I don’t know why it didn’t work; secondly,
I used the maven-shade-plugin plugin to exclude this class separately, various attempts After various information checks, various errors were reported when the project started;
finally, I used the @ComponentScan annotation to exclude this class, and the usage method is as follows:

@ComponentScan(basePackages={
    
    "cn.com.compass.core"},
		excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = {
    
    "cn.com.compass.core.handler.BaseExceptionHandler"}))
@SpringBootApplication
public class Application {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(App.class, args);
	}

}

Introduction to @ComponentScan annotation

1. The role of @ComponentScan annotation

The @ComponentScan annotation is generally used together with the @Configuration annotation. The main function is to define the rules of package scanning, and then find out which classes need to be automatically assembled into the spring bean container according to the defined rules, and then handed over to spring for unified management.

Explanation: All classes marked with @Controller, @Service, @Repository, and @Component can be scanned by spring.

2. Introduction to @ComponentScan annotation properties

2.1 value

Specify the package path to scan

2.2 excludeFilters (exclusion rules)

excludeFilters=Filter[] Specify the components to be excluded according to the rules when specifying package scanning

2.3 includeFilters (include rules)

includeFilters =Filter[] Specify the components to be included according to the rules when specifying package scanning.

Note: You need to set useDefaultFilters = false (the system defaults to true, you need to set it manually) includeFilters includes filtering rules to take effect.

2.4 FilterType property

FilterType.ANNOTATION: filter by annotation

FilterType.ASSIGNABLE_TYPE: According to the given type, specify a specific class, and subclasses will also be scanned

FilterType.ASPECTJ: Use ASPECTJ expressions

FilterType.REGEX: regular

FilterType.CUSTOM: custom rules

useDefaultFilters: Whether the configuration is enabled can detect classes annotated with @Component, @Repository, @Service, and @Controller. For Java8 syntax, multiple @ComponentScan can be specified. Below Java8, @ComponentScans() can be used to configure multiple rules

conclusion

The above is the processing process of this error report. If your project is also reported to have duplicate class names at startup, I hope my processing method will be useful to you.

Guess you like

Origin blog.csdn.net/weixin_42717648/article/details/128582047