溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot啟動怎么排除某些bean的注入

發(fā)布時間:2021-08-02 13:39:20 來源:億速云 閱讀:1273 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“springboot啟動怎么排除某些bean的注入”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“springboot啟動怎么排除某些bean的注入”吧!

springboot 啟動排除某些bean的注入

問題:

最近做項(xiàng)目的時候,需要引入其他的jar。然后還需要掃描這些jar里的某些bean。于是使用注解:@ComponentScan

這個注解直接指定包名就可以,它會去掃描這個包下所有的class,然后判斷是否解析:

@ComponentScan(basePackages = {"your.pkg","other.pkg"})
public class Application {
} 

其他的jar中定義了 redissonConfig 這個bean。然后我自己的項(xiàng)目也定義了redissonConfig 這個bean。導(dǎo)致項(xiàng)目啟動報錯。所以使用如下方式,排除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、這個注解修飾的類是不會被掃描到的。這個時候我們就得自定義掃描器完成這個操作。

配置文件中使用的:

component-scan標(biāo)簽底層使用ClassPathBeanDefinitionScanner這個類完成掃描工作的。@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ù)正則過濾

到此,相信大家對“springboot啟動怎么排除某些bean的注入”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI