欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中TypeExcludeFilter的作用及使用方式

 更新時(shí)間:2025年01月06日 09:19:20   作者:catoop  
在SpringBoot應(yīng)用程序中,TypeExcludeFilter通過過濾特定類型的組件,使它們不被自動掃描和注冊為bean,這在排除不必要的組件或特定實(shí)現(xiàn)類時(shí)非常有用,通過創(chuàng)建自定義過濾器并注冊到spring.factories文件中,我們可以在應(yīng)用啟動時(shí)生效

SpringBoot中TypeExcludeFilter的作用及使用

在Spring Boot應(yīng)用程序中,TypeExcludeFilter 是一個(gè)用于過濾特定類型的組件,使之不被Spring容器自動掃描和注冊為bean的工具。

這在你想要排除某些類或類型(如配置類、組件等)而不希望它們參與Spring的自動裝配時(shí)非常有用。

作用

通常情況下,Spring Boot應(yīng)用會通過包掃描的方式自動識別并加載帶有@Component, @Service, @Repository, 或者@Configuration等注解的類。

然而,在一些場景下,我們可能不希望某些特定的類被自動加載,例如測試環(huán)境中的特殊實(shí)現(xiàn),或者是為了優(yōu)化啟動速度而排除不必要的組件。

此時(shí),TypeExcludeFilter就派上用場了,它允許開發(fā)者根據(jù)類型來排除這些不需要的組件。

使用示例

假設(shè)我們有一個(gè)項(xiàng)目結(jié)構(gòu)如下:

com.example
├── config
│   └── AppConfig.java
└── service
│   ├── HelloService.java
│   └── WorldService.java
└── MyApplication.java

這兩個(gè) Service 默認(rèn)都會被 SpringBootApplication 自動掃描。

我們可以利用TypeExcludeFilter來確保MockService不會在非測試環(huán)境中被加載。

首先,我們需要創(chuàng)建一個(gè)自定義的過濾器,該過濾器繼承自TypeExcludeFilter并重寫matches方法來指定哪些類型應(yīng)該被排除。

請注意:

  • 我們繼承 TypeExcludeFilter 并重寫方法而不是直接實(shí)現(xiàn)TypeFilter接口中的matches方法。
  • 這是因?yàn)?code>TypeExcludeFilter 是 @SpringBootApplication 注解自動掃描時(shí)候已經(jīng)默認(rèn)配置的過濾器類

如下是源碼片段:

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
   ...
}

編寫自定義過濾器

創(chuàng)建自定義過濾器類,繼承 TypeExcludeFilter 并重寫 match 方法:

package com.example.filter;

import org.springframework.context.annotation.TypeExcludeFilter;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;

public class MyTypeExcludeFilter extends TypeExcludeFilter {

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        String className = metadataReader.getClassMetadata().getClassName();
        // 根據(jù)需要設(shè)置排除邏輯,例如這里排除特定類。比如你還可以排除所有 Test開頭、Demo開頭這樣的類。
        return className.startsWith("com.example.service.HelloService");
    }
}

注冊自定義過濾器

為了確保我們的自定義過濾器能夠在應(yīng)用啟動時(shí)生效,我們需要創(chuàng)建一個(gè)實(shí)現(xiàn)了ApplicationContextInitializer接口的類,并在此類中注冊我們的過濾器。

package com.example.initializer;

import com.example.filter.MyTypeExcludeFilter;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.TypeFilter;

public class MyTypeExcludeFilterInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(@NonNull ConfigurableApplicationContext applicationContext) {
        applicationContext.getBeanFactory().registerSingleton("myTypeExcludeFilter", new MyTypeExcludeFilter());
    }
}

配置 spring.factories

最后一步是在META-INF/spring.factories文件中注冊我們的ApplicationContextInitializer,這樣它就可以在應(yīng)用啟動時(shí)被自動加載:

org.springframework.context.ApplicationContextInitializer=\
com.example.initializer.MyTypeExcludeFilterInitializer

這行配置告訴Spring Boot,在應(yīng)用啟動時(shí),應(yīng)該查找并初始化MyTypeExcludeFilterInitializer。

之所以需要在 spring.factories 中進(jìn)行配置,是因?yàn)槲覀冃枰?@ComponentScan 注解執(zhí)行之前就將我們的自定義過濾器類注冊到 spring 上下文中,否則自定義的類就沒用了。

總結(jié)

通過上述步驟,我們成功地將TypeExcludeFilter集成到了Spring Boot應(yīng)用中,并且不需要顯式地編寫@ComponentScan注解或修改任何現(xiàn)有的配置。

這種方式不僅簡化了代碼結(jié)構(gòu),也使得我們可以更加靈活地控制哪些類型的組件應(yīng)該被排除在外。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論