如果在springboot项目中删除其他jar包的Bean
今天在项目中遇到这么一个问题,引入公司别等团队的jar包,但是该包中有一个拦截器????,使系统的多语问题失效。。。。。怎么办,开始想通过在启动类中删除该类@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,pattern = {"com.xx.Processor","com.xx.Parti
今天在项目中遇到这么一个问题,引入公司别等团队的jar包,但是该包中有一个拦截器😂,使系统的多语问题失效。。。。。
怎么办,开始想通过在启动类中删除该类
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
pattern = {"com.xx.Processor","com.xx.PartitionConfig"})})
这种方法没有效果,后来才知道,项目包以外的bean一般是通过 通过spring SPI spring.factories的方法把Bean加载到另一个项目当中去。
spring.factories会创建一些jar中的定义的bean,比如强制格式化返回值
我这里遇到的坑是不想要该Bean,想删除他!!!后来问了大佬才知道能够通过使用BeanDefinitionRegistryPostProcessor,直接在 解析完bean的注册信息后,直接移除就行,这样就不会创建bean。BeanDefinitionRegistryPostProcessor继承了
BeanFactoryPostProcessor能够管理这些bean
@Component
public class RemoveRegistryBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
if (registry.containsBeanDefinition("upesnWebCommonConfiguration")) {
registry.removeBeanDefinition("upesnWebCommonConfiguration");
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
spi 在spring.factory中加入 就会被扫描进去

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)