Spring Boot 排除某個類加載註入IOC的操作

Spring Boot 排除某個類加載註入IOC

我們項目往往會引入其他項目的依賴,造成功能沖突的類,我們想把這些類排除掉,不註入到我們項目IoC容器中,

隻加載自己的類

@ComponentScan(basePackages = "com.xxx",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {
                xxxPublisher.class,
                xxxAdvice.class,
               xxxService.class})})

其中這三個類,我不需要加載到我們項目中,需要指明type=FilterType.ASSIGNABLE_TYPE

不指定type類型執行classes={xxx…} 排除不瞭

它有五種類型:

public enum FilterType {
    ANNOTATION, 
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM;
}

spring boot 排除個別配置類的代碼

廢話不說,直接上代碼

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableScheduling
@ComponentScan(basePackages = {"com.hudai.platform.sms.vendor","com.hudai.platform.scp"}, excludeFilters =
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.hudai.platform.scp.alert.config.RestTemplateConfig.class))
public class SmsVendorApplication {

    public static void main(String[] args) {
        SpringApplication.run(SmsVendorApplication.class, args);
    }
}
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.hudai.platform.scp.alert.config.RestTemplateConfig.class))

這段是經典~

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: