springboot 啟動(dòng)如何排除某些bean的注入
最近做項(xiàng)目的時(shí)候,需要引入其他的jar。然后還需要掃描這些jar里的某些bean。于是使用注解:@ComponentScan
這個(gè)注解直接指定包名就可以,它會(huì)去掃描這個(gè)包下所有的class,然后判斷是否解析:
@ComponentScan(basePackages = {'your.pkg','other.pkg'})public class Application {}
其他的jar中定義了 redissonConfig 這個(gè)bean。然后我自己的項(xiàng)目也定義了redissonConfig 這個(gè)bean。導(dǎo)致項(xiàng)目啟動(dòng)報(bào)錯(cuò)。所以使用如下方式,排除jar 中的RedissonConfig.class。
@ComponentScan(basePackages = {'com.xx.xx.*'}, excludeFilters =@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {RedissonConfig.class}))@ComponentScan注解
掃描或解析的bean只能是Spring內(nèi)部所定義的,比如@Component、@Service、@Controller或@Repository。如果有一些自定義的注解,比如@Consumer、這個(gè)注解修飾的類是不會(huì)被掃描到的。這個(gè)時(shí)候我們就得自定義掃描器完成這個(gè)操作。
配置文件中使用的:component-scan標(biāo)簽底層使用ClassPathBeanDefinitionScanner這個(gè)類完成掃描工作的。@ComponentScan注解配合@Configuration注解使用,底層使用ComponentScanAnnotationParser解析器完成解析工作。
springboot排除掃描包@SpringBootApplication@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX,pattern = 'com.action.other.*') })public class Application { public static void main(String[] args) {SpringApplication.run(Application.class, args); }}
根據(jù)FilterType不同有不同的過濾方式,這里是根據(jù)正則過濾
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程2. 詳解瀏覽器的緩存機(jī)制3. Xml簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. python多線程和多進(jìn)程關(guān)系詳解5. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解6. Python xlrd/xlwt 創(chuàng)建excel文件及常用操作7. python 寫函數(shù)在一定條件下需要調(diào)用自身時(shí)的寫法說明8. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼9. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)10. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享
