前言
公司有個項目,時間比較趕,而且專案的部分需求,和之前做的專案部分功能一樣,為了趕速度和直接將之前多模組的maven專案中的部分模組,直接以jar包的形式引入到新項目中了,雖然省去了不少開發時間,但是造成項目需要導入引入項目jar的相關依賴,導致項目臃腫,啟動很慢。有沒有辦法讓專案只載入自己需要的bean呢?
當然我們可以直接修改原始碼重新打包引入去解決,但是這個辦法太多麻煩。
透過百度的手段,查詢可以在springboot啟動類別上用@ComponentScan註解去實現
程式碼範例
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = {"com.xx.controller","com.xx.xx"})})
但是經過實作很多次發現沒用,原來項目套件以外的bean一般是透過spring SPI spring.factories的方法把Bean載入到另一個專案當中去。
spring.factories會建立一些jar中的定義的bean,例如強制格式化回傳值
後來發現透過使用BeanDefinitionRegistryPostProcessor,直接在解析完bean的註冊資訊後,直接移除就行,這樣就不會創建bean。
BeanDefinitionRegistryPostProcessor繼承BeanFactoryPostProcessor能夠管理這些bean
實作程式碼範例
在專案中新建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 { } }
當然還有部分自動設定類別的程式碼需要刪除bean註冊,我們可以透過
@SpringBootApplication(exclude = {})的方式去实现,代码如下: @EnableAsync @EnableScheduling @SpringBootApplication(exclude = {DllInitLoader.class,ProcessEngineServicesAutoConfiguration.class}) public class UnifyWorkFaceApplication { public static void main (String[] args) { BladeApplication.run("work-face", UnifyWorkFaceApplication.class, args); } }
配置完畢,專案啟動速度快了很多,也去除了很多jar依賴,還刪除了很多無用表,例如flowable工作流程的相關表,先前專案啟動時老是自動去查詢工作流程的相關表,導致刪除資料庫的工作流程的表格專案就會啟動不起來,現在透過
@SpringBootApplication(exclude = {ProcessEngineServicesAutoConfiguration.class}) 移除ProcessEngineServicesAutoConfiguration自動配置類別代碼,還有剔除,專案中引用flowable的專案類別的注入,就可以正常啟動了。
以上是SpringBoot怎麼刪除引用jar包中的無用bean的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中