【sprinb-boot】排除/不加载某些Bean
前言
- springboot 2.0.0.RELEASE
- maven 3.5.0
- 這里介紹內(nèi)容為,在spring boot啟動時,排除/不加載某些Bean。spring boot啟動時,排除/不加載某些模塊/AutoConfiguration類,看這里。
排除/不加載某些Bean
排除/不加載某些Bean時,需要使用@ComponentScan。具體做法,可以采用下面的方式:
- 方式1:自定義 @ComponentScan
- 方式2:自定義 @ComponentScans
- 方式3:自定義 TypeExcludeFilter
方式1:自定義 @ComponentScan
假設(shè):我在使用 RuoYi 的時候,想自己的實現(xiàn) ShiroConfig,而不用 RuoYi 自帶的 ShiroConfig ,且,不刪除 RuoYi 自帶的 ShiroConfig 類。
此種情況下,就需要啟動項目時,不加載 ShiroConfig 類。
針對此種情況,可以這樣做:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @ComponentScan(excludeFilters = {@Filter(type = FilterType.REGEX, pattern = {"com.ruoyi.framework.config.ShiroConfig"})}) public class RuoYiApplication {... }說明:
- 使用自定義的 @ComponentScan 注解替換 @SpringBootApplication 的注解。
- 定義 excludeFilters 屬性。該屬性指定排除的Bean/類。
- 使用正則表達(dá)式方式( FilterType.REGEX )排除類 "com.ruoyi.framework.config.ShiroConfig"
方式2:自定義 @ComponentScans
@SpringBootApplication 有些特殊,官方的解釋是:same as @Configuration @EnableAutoConfiguration @ComponentScan。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {... }你看!@SpringBootApplication 帶了一個 @ComponentScan。一個 @ComponentScan 會影響 @ComponentScans(為啥?暫不清楚)。
所以,自定義 @ComponentScans 是應(yīng)該這樣:
@Configuration @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) @ComponentScans({@ComponentScan(basePackages = {"com.ruoyi"}, excludeFilters = {@Filter(type = FilterType.REGEX, pattern = {"com.ruoyi.framework.config.ShiroConfig"})}),@ComponentScan(basePackages = {"com.third.package"}), }) public class RuoYiApplication {... }說明:
- 使用@Configuration @EnableAutoConfiguration @ComponentScans 替換了 @SpringBootApplication。
- 使用 @ComponentScans 是為了添加多個 @ComponentScan。只有一個 @ComponentScan 時,可以不用 @ComponentScans。
方式3:自定義 TypeExcludeFilter
@SpringBootApplication 有些特殊,它帶了一個 @ComponentScan。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {... }你看!@SpringBootApplication 自帶的 @ComponentScan 中已經(jīng)有了自定義的 excludeFilters。可以利用此特性實現(xiàn)排除/不加載某些Bean。
在 src/main/resources 目錄下創(chuàng)建文件META-INF/spring.factories(如果該文件已存在,則無須創(chuàng)建)。在文件META-INF/spring.factories中添加內(nèi)容:
說明:操作復(fù)雜,實測效果不理想,不推薦使用。
參考
https://www.cnblogs.com/yql1986/p/9418419.html
https://stackoverflow.com/questions/41287276/when-should-i-use-typeexcludefilters-in-spring
https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-excluding-config
總結(jié)
以上是生活随笔為你收集整理的【sprinb-boot】排除/不加载某些Bean的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何使用农行掌银购买农银快e宝
- 下一篇: nginx loaction