Spring Boot 项目启动排除一些配置类

起因:在改造未曾谋面的同事的微服务遇到的尴尬问题,发现公共的基础项目中已经配置了RedisConfig,本地的微服务项目中也配置这样一个配置。

一、配置代码如下
@Component
@EnableCaching
public class MyRedisConfig extends CachingConfigurerSupport {
    //省略代码
}
二、我的难题

错误信息

Caused by: java.lang.IllegalStateException: 2 implementations of 
CachingConfigurer were found when only 1 was expected. Refactor 
the configuration such that CachingConfigurer is implemented only 
once or not at all. at
三、原由

spring中允许使用默认的cacheManager,但是不允许同时存在两个。

四、排除其他的

那就在启动类上使用注解过滤不需要重复的文件,关键代码如下。

@ComponentScan(
        basePackages = {"com.xx"},
        excludeFilters= {@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,
        classes= {com.xx.common.config.CacheConfig.class,com.xx.common.config.RedisConfig.class})})

其实也可以直接删除自己维护项目中的配置,只是因为不确定删除之后会出现什么状态,所以我们选择排除其他的配置。

你可能感兴趣的:(Spring Boot 项目启动排除一些配置类)