日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【sprinb-boot】排除/不加载某些Bean

發(fā)布時間:2024/9/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【sprinb-boot】排除/不加载某些Bean 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

  • 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??梢岳么颂匦詫崿F(xiàn)排除/不加載某些Bean。

  • 實現(xiàn)自己的 TypeExcludeFilter
  • public class MyTypeExcludeFilter extends TypeExcludeFilter {@Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)throws IOException {if("com.ruoyi.framework.config.ShiroConfig".equals(metadataReader.getClassMetadata().getClassName())){System.out.println("skip >>>>>>> " + metadataReader.getClassMetadata().getClassName());return true;}return false;}}
  • 將 MyTypeExcludeFilter 在 ApplicationContext Initializer 時注冊Bean(通過注解注冊的Bean,進(jìn)入 Spring 容器太晚了,不起作用)。
  • public class MyTypeExcludeFilterApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {applicationContext.addBeanFactoryPostProcessor(new MyTypeExcludeFilterPostProcessor());}private static class MyTypeExcludeFilterPostProcessorimplements PriorityOrdered, BeanDefinitionRegistryPostProcessor {public static final String BEAN_NAME = "com.ruoyi." + "myTypeExcludeFilter";@Overridepublic int getOrder() {return Ordered.HIGHEST_PRECEDENCE;}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {RootBeanDefinition definition = new RootBeanDefinition(MyTypeExcludeFilter.class);registry.registerBeanDefinition(BEAN_NAME, definition);} } }
  • 注冊 MyTypeExcludeFilterApplicationContextInitializer
    在 src/main/resources 目錄下創(chuàng)建文件META-INF/spring.factories(如果該文件已存在,則無須創(chuàng)建)。在文件META-INF/spring.factories中添加內(nèi)容:
  • org.springframework.context.ApplicationContextInitializer=\ com.ruoyi.MyTypeExcludeFilterApplicationContextInitializer

    說明:操作復(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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。