SpringBoot项目删除Bean或者不加载Bean的问题解决
更新时间:2025年01月13日 16:47:56 作者:抹灰丶
文章介绍了在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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
springboot springmvc抛出全局异常的解决方法
这篇文章主要为大家详细介绍了springboot springmvc抛出全局异常的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-06-06
详解Spring boot/Spring 统一错误处理方案的使用
这篇文章主要介绍了详解Spring boot/Spring 统一错误处理方案的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-06-06
SpringCloud如何创建一个服务提供者provider
这篇文章主要介绍了SpringCloud如何创建一个服务提供者provider,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-07-07


最新评论