詳解SpringBoot如何刪除引用jar包中的無用bean
前言
公司有個(gè)項(xiàng)目,時(shí)間比較趕,而且項(xiàng)目的部分需求,和之前做的項(xiàng)目部分功能一樣,為了趕速度和直接將之前多模塊的maven項(xiàng)目中的部分模塊,直接以jar包的形式引入到新項(xiàng)目中了,雖然省去了不少開發(fā)時(shí)間,但是造成項(xiàng)目需要導(dǎo)入引入項(xiàng)目jar的相關(guān)依賴,導(dǎo)致項(xiàng)目臃腫,啟動(dòng)很慢。有沒有辦法讓項(xiàng)目只加載自己需要的bean呢?
當(dāng)然我們可以直接修改源代碼重新打包引入去解決,但是這個(gè)辦法太多麻煩。
通過百度的手段,查詢可以在springboot啟動(dòng)類上用@ComponentScan注解去實(shí)現(xiàn)
代碼示例
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
pattern = {"com.xx.controller","com.xx.xx"})})
但是經(jīng)過實(shí)現(xiàn)很多次發(fā)現(xiàn)沒用,原來項(xiàng)目包以外的bean一般是通過 通過spring SPI spring.factories的方法把Bean加載到另一個(gè)項(xiàng)目當(dāng)中去。
spring.factories會(huì)創(chuàng)建一些jar中的定義的bean,比如強(qiáng)制格式化返回值
后來發(fā)現(xiàn)通過使用BeanDefinitionRegistryPostProcessor,直接在 解析完bean的注冊(cè)信息后,直接移除就行,這樣就不會(huì)創(chuàng)建bean。
BeanDefinitionRegistryPostProcessor繼承BeanFactoryPostProcessor能夠管理這些bean
實(shí)現(xiàn)代碼示例
在項(xiàng)目中新建 RemoveRegistryBeanFactoryPostProcessor類,代碼如下
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;
/**
* @author liuya
*/
@Component
public class RemoveRegistryBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
String[] names = registry.getBeanDefinitionNames();
for (String name : names) {
if (name.contains("taskSendMessageListener") || name.contains("globalListener") || name.contains("executionSendMessageListener") || name.contains("processCallbackMesController")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.system.flow")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.system.modules.message")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.graphics.task")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.graphics.bimlight.location")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.graphics.bimlight.sectioning")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.graphics.modules")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.ubw.job")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.ubw.listener")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.ubw.msg")) {
registry.removeBeanDefinition(name);
}
if (name.contains("org.springblade.ubw.service")) {
registry.removeBeanDefinition(name);
}
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
當(dāng)然還有部分自動(dòng)配置類的代碼需要?jiǎng)h除bean注冊(cè),我們可以通過
@SpringBootApplication(exclude = {})的方式去實(shí)現(xiàn),代碼如下:
@EnableAsync
@EnableScheduling
@SpringBootApplication(exclude = {DllInitLoader.class,ProcessEngineServicesAutoConfiguration.class})
public class UnifyWorkFaceApplication {
public static void main (String[] args) {
BladeApplication.run("work-face", UnifyWorkFaceApplication.class, args);
}
}
配置完畢,項(xiàng)目啟動(dòng)速度快了很多,也去除了很多jar依賴,還刪除了很多無用表,比如flowable工作流的相關(guān)表,之前項(xiàng)目啟動(dòng)時(shí)老是自動(dòng)去查詢工作流的相關(guān)表,導(dǎo)致刪除數(shù)據(jù)庫的工作流的表項(xiàng)目就會(huì)啟動(dòng)不起來,現(xiàn)在通過
@SpringBootApplication(exclude = {ProcessEngineServicesAutoConfiguration.class}) 移除ProcessEngineServicesAutoConfiguration自動(dòng)配置類代碼,還有剔除,項(xiàng)目中引用flowable的項(xiàng)目類的注入,就可以正常啟動(dòng)了。
以上就是詳解SpringBoot如何刪除引用jar包中的無用bean的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot刪除無用bean的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
myBatis組件教程之緩存的實(shí)現(xiàn)與使用
這篇文章主要給大家介紹了關(guān)于myBatis組件教程之緩存的實(shí)現(xiàn)與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
SpringBoot @Autowired注解注入規(guī)則介紹
這篇文章主要介紹了SpringBoot @Autowired注解注入規(guī)則介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-11-11
MyBatis-Plus之邏輯刪除的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus之邏輯刪除的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
關(guān)于String轉(zhuǎn)Json的幾種方式
這篇文章主要介紹了關(guān)于String轉(zhuǎn)Json的幾種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
JAVA設(shè)置手動(dòng)提交事務(wù),回滾事務(wù),提交事務(wù)的操作
這篇文章主要介紹了JAVA設(shè)置手動(dòng)提交事務(wù),回滾事務(wù),提交事務(wù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
多用多學(xué)之Java中的Set,List,Map詳解
下面小編就為大家?guī)硪黄嘤枚鄬W(xué)之Java中的Set,List,Map詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06

