报错日志如下:
Caused by:org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'globalExceptionHandler' for bean class [com.cheng.cloud.common.exception.handler.GlobalExceptionHandler] conflicts with existing, non-compatible bean definition of same name and class [com.cheng.media.exception.handler.GlobalExceptionHandler] 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:287) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ... 21 common frames omitted
通过日志可以看出是因为spring容器初始化同名不同包的类时,不知道加载哪一个,而导致的冲突。
因此,我们需要解决冲突,在启动类Application中去除一个类。
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@ComponentScan(basePackages = {"com.cheng.media", "com.cheng.cloud"},
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {GlobalExceptionHandler.class}))
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}