原创

SpringBoot-ComponentScan-排除jar包中的某个类

//@ComponentScan教程https://blog.csdn.net/winterking3/article/details/114699603
@ComponentScan(value = {"cn.jiangjiesheng.edu","cn.jiangjiesheng.modules.filestore"},
        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, 
        classes = cn.jiangjiesheng.service.PaasUserService.class))

支持多个


@ComponentScan(value = {"cn.jiangjiesheng"},excludeFilters = 
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, 
classes = {ApaasAutoConfiguration.class, CronTools.class}))

支持自定义:


@ComponentScan(value = {"cn.jiangjiesheng"},excludeFilters = 
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {ExcludeTypeFilter.class}))
package cn.jiangjiesheng.fst.config.bean;

import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

public class ExcludeTypeFilter implements TypeFilter {

    @Override
    public boolean match(MetadataReader metadataReader, 
    MetadataReaderFactory metadataReaderFactory) throws IOException {
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        String className = classMetadata.getClassName();
        //一般是全包名的
        //System.out.println("--类名--:"+className);

        for (String keyWord : RemoveBean.REMOVE_BEAN_KEYWORD_LIST) {
            boolean contains = className.toLowerCase().contains(keyWord);
            if (contains) {
                System.out.println("本次移除了:"+ className);
                return true;
            }
        }
        return false;
    }
}

另外一个移除bean的

package cn.jiangjiesheng.fst.config.bean;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RemoveRegistyBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    //RefreshAutoConfiguration internalEventListenerFactory
    //TODO 这个还是要加个开关判断的
    //注意写入全小写的


    //移出bean。 aa是bean的名称 https://blog.csdn.net/woyixinyiyi/article/details/109577135
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) 
    throws BeansException {
        String[] beanDefinitionNames = beanDefinitionRegistry.getBeanDefinitionNames();
//        System.out.println("beanDefinitionNames:" + JSON.toJSONString(beanDefinitionNames));
//        if (beanDefinitionRegistry.containsBeanDefinition("nacosDiscoveryEndpointAutoConfiguration")) {
//            beanDefinitionRegistry.removeBeanDefinition("nacosDiscoveryEndpointAutoConfiguration");
//        }

        //因为有的bean是全包名的com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration,
        且和控制台中报错的堆栈类名不完全一样。
        //所以还是按关键字来移除吧
        for (String beanDefinitionName : beanDefinitionNames) {
            for (String keyWord : RemoveBean.REMOVE_BEAN_KEYWORD_LIST) {
                boolean contains = beanDefinitionName.contains(keyWord);
                if (contains) {
                    beanDefinitionRegistry.removeBeanDefinition(beanDefinitionName);
                    System.out.println("本次移除了:"+ beanDefinitionName);
                }
            }
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) 
    throws BeansException {

    }
}
package cn.jiangjiesheng.fst.config.bean;

import com.google.common.collect.Lists;

import java.util.List;

public class RemoveBean {
    public final static List<String> REMOVE_BEAN_KEYWORD_LIST = Lists.newArrayList(
            "nacos",
            "dubbo",
            "braveautoconfiguration",
            "apaasautoconfiguration",
            "crontools",
            "dubboautoconfiguration",
            "zipkinautoconfiguration"
    );
}
正文到此结束
本文目录