SpringBoot排除某类注入IOC Spring Boot 排除某个类加载注入IOC的操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot排除某类注入IOC Spring Boot 排除某个类加载注入IOC的操作

以后的今天   2021-08-02 我要评论
想了解Spring Boot 排除某个类加载注入IOC的操作的相关内容吗,以后的今天在本文为您仔细讲解SpringBoot排除某类注入IOC的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot排除某类,类注入,注入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))

这段是经典~

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们