一、緣起
由於公司把redis相關的配置類,工具類放在了一個類似common的工程里,這樣以后肯定不可避免的出現某些項目可能並不需要使用redis,但是還是依賴common里的別的一些類庫
所以排除springboot啟動加載的一些bean還是有意義的
二、@ComponenScan注解
@ComponentScan注解用來掃描加排除,不加ComponentScan注解,springboot是默認掃描springboot啟動類所在的包及其子包,我們現在自己掃描,然后使用@ComponentScan注解的excludeFilters屬性用來排除不想掃描的bean。
我的示例是排除redis相關的配置,光排除自己寫的配置類時不行的,還需要排除springboot提供的自動配置類,使用@SpringBootApplication的exclude屬性把RedisAutoConfiguration.class排除掉,這個時候啟動,結果尷尬了,報錯一個組件依賴了redisTemplate這個bean。找了挺久,原來還要把RedisRepositoriesAutoConfiguration去掉
代碼如下
@SpringBootApplication(exclude = {RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class}) @EnableEurekaClient @EnableCircuitBreaker @EnableDiscoveryClient @EnableTransactionManagement @ComponentScan(value = "com.aw.phjr", excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {RedisConfiguration.class, RedisUtil.class})) public class RestRpc_Application {
如果想知道配置redis或者其他組件都有哪些自動配置類,可以參考文章:https://www.cnblogs.com/xhy-shine/p/11274906.html