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

SpringBoot項(xiàng)目刪除Bean或者不加載Bean的問題解決

 更新時(shí)間:2025年01月13日 16:47:56   作者:抹灰丶  
文章介紹了在Spring Boot項(xiàng)目中如何使用@ComponentScan注解和自定義過濾器實(shí)現(xiàn)不加載某些Bean的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧

使用@ComponentScan注解中的@ComponentScan.Filter標(biāo)記不加載。

@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASPECTJ, pattern ={"包名"})})
@ComponentScan(excludeFilters  = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = 類名.class)})

實(shí)現(xiàn)BeanFactoryPostProcessor接口,編譯完畢后刪除 (當(dāng)然這里你也可以寫一個(gè)配置類)

@SpringBootApplication
public class EmpServiceApplication implements BeanFactoryPostProcessor {
    public static void main(String[] args) {
        SpringApplication.run(EmpServiceApplication.class, args);
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                // 檢查是否是 BeanDefinitionRegistry
                if (beanFactory instanceof BeanDefinitionRegistry) {
                    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
                    // 獲取所有Bean的名稱
                    String[] beanNames = beanFactory.getBeanDefinitionNames();
                    for (String beanName : beanNames) {
                        // 獲取Bean的定義
                        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
                        // 獲取Bean的類名
                       String beanClassName = beanDefinition.getBeanClassName();
                        // 自定義排除邏輯
                        if (beanClassName != null && beanClassName.startsWith("包名")) {
                            // 移除不需要的Bean
                            registry.removeBeanDefinition(beanName);
                            System.out.println("Excluded bean: " + beanName);
                        }}} 
                else {
            throw new IllegalStateException("BeanFactory is not a BeanDefinitionRegistry");
        }
    }
}

使用@ComponentScan,配合自定義過濾器,實(shí)現(xiàn)TypeFilter接口,指定不編譯不加載某些Bean

@SpringBootApplication
@ComponentScan(excludeFilters = @ComponentScan.Filter(
    // 使用自定義過濾器               
    type = FilterType.CUSTOM, 
    // 指定自定義過濾器類
    classes = CustomExcludeFilter.class))
public class ServiceApplication{
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
  }
}
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
/**
 * @Description: 自定義排除過濾器:實(shí)現(xiàn)自定義的排除邏輯,返回true表示排除該類,返回false表示包含該類。
 * @Version: 1.0
 **/
public class CustomExcludeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
        // 在這里實(shí)現(xiàn)自定義的排除邏輯。例如,根據(jù)類的名稱、包名或其他屬性來決定是否排除該類。這里獲得是類的全限定名。版本升級(jí)請(qǐng)注意
        String className = metadataReader.getClassMetadata().getClassName();
        if (className != null && className.startsWith("包名")) {
            // 返回true表示排除該類。
            return true;
        }
        // 返回false表示包含該類。
        return false;
    }
}

到此這篇關(guān)于SpringBoot項(xiàng)目刪除Bean或者不加載Bean的文章就介紹到這了,更多相關(guān)SpringBoot項(xiàng)目不加載Bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Java環(huán)境變量配置方法(Windows)

    詳解Java環(huán)境變量配置方法(Windows)

    這篇文章主要介紹了Java環(huán)境變量配置方法(Windows),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • javaWeb項(xiàng)目部署到阿里云服務(wù)Linux系統(tǒng)的詳細(xì)步驟

    javaWeb項(xiàng)目部署到阿里云服務(wù)Linux系統(tǒng)的詳細(xì)步驟

    這篇文章主要介紹了javaWeb項(xiàng)目部署到阿里云服務(wù)Linux系統(tǒng),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • springboot springmvc拋出全局異常的解決方法

    springboot springmvc拋出全局異常的解決方法

    這篇文章主要為大家詳細(xì)介紹了springboot springmvc拋出全局異常的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • java實(shí)現(xiàn)的RC4加密解密算法示例

    java實(shí)現(xiàn)的RC4加密解密算法示例

    這篇文章主要介紹了java實(shí)現(xiàn)的RC4加密解密算法,結(jié)合具體實(shí)例形式分析了java RC4加密解密算法的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2017-06-06
  • 詳解Spring boot/Spring 統(tǒng)一錯(cuò)誤處理方案的使用

    詳解Spring boot/Spring 統(tǒng)一錯(cuò)誤處理方案的使用

    這篇文章主要介紹了詳解Spring boot/Spring 統(tǒng)一錯(cuò)誤處理方案的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • SpringCloud如何創(chuàng)建一個(gè)服務(wù)提供者provider

    SpringCloud如何創(chuàng)建一個(gè)服務(wù)提供者provider

    這篇文章主要介紹了SpringCloud如何創(chuàng)建一個(gè)服務(wù)提供者provider,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • 基于RocketMQ實(shí)現(xiàn)分布式事務(wù)的方法

    基于RocketMQ實(shí)現(xiàn)分布式事務(wù)的方法

    了保證系統(tǒng)數(shù)據(jù)的一致性,我們需要確保這些服務(wù)中的操作要么全部成功,要么全部失敗,通過使用RocketMQ實(shí)現(xiàn)分布式事務(wù),我們可以協(xié)調(diào)這些服務(wù)的操作,保證數(shù)據(jù)的一致性,這篇文章主要介紹了基于RocketMQ實(shí)現(xiàn)分布式事務(wù),需要的朋友可以參考下
    2024-03-03
  • SpringBoot+Vue添加騰訊云人臉識(shí)別的項(xiàng)目實(shí)踐

    SpringBoot+Vue添加騰訊云人臉識(shí)別的項(xiàng)目實(shí)踐

    人臉識(shí)別是一種基于人臉特征進(jìn)行身份認(rèn)證和識(shí)別的技術(shù),本文主要介紹了SpringBoot+Vue添加騰訊云人臉識(shí)別的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • 基于java中集合的概念(詳解)

    基于java中集合的概念(詳解)

    下面小編就為大家?guī)硪黄趈ava中集合的概念(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java實(shí)現(xiàn)簡(jiǎn)單的遞歸操作方法實(shí)例

    Java實(shí)現(xiàn)簡(jiǎn)單的遞歸操作方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)簡(jiǎn)單的遞歸操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評(píng)論