java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot项目不加载Bean

SpringBoot项目删除Bean或者不加载Bean的问题解决

作者:抹灰丶

文章介绍了在Spring Boot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧

使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。

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

实现BeanFactoryPostProcessor接口,编译完毕后删除 (当然这里你也可以写一个配置类)

@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,配合自定义过滤器,实现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: 自定义排除过滤器:实现自定义的排除逻辑,返回true表示排除该类,返回false表示包含该类。
 * @Version: 1.0
 **/
public class CustomExcludeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
        // 在这里实现自定义的排除逻辑。例如,根据类的名称、包名或其他属性来决定是否排除该类。这里获得是类的全限定名。版本升级请注意
        String className = metadataReader.getClassMetadata().getClassName();
        if (className != null && className.startsWith("包名")) {
            // 返回true表示排除该类。
            return true;
        }
        // 返回false表示包含该类。
        return false;
    }
}

到此这篇关于SpringBoot项目删除Bean或者不加载Bean的文章就介绍到这了,更多相关SpringBoot项目不加载Bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文