springboot啟動的時候排除加載某些bean


一、緣起

由於公司把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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM