spring boot排除扫描类的三种方式

最近在做单测的时候,由于自己配置的spring boot容器会默认扫描很多不想被加载,网上中文的文章并不多,所以来总结一下。


默认下面描述的类都在一个包下面。

  • 第一步我们新建一个应用启动的类,一个类用来充当Configuration,为了能明显的感知到其到底有没生效,我编写如下:
@SpringBootApplication
public class Test  {
    public static void main(String[] args) {
        new SpringApplication(Test.class).run(args);
    }
}
@Configuration
public class MyConfig {

    @Bean
    public BeanPostProcessor beanPostProcessor() {
        System.out.println("初始化了 bean BeanPostProcessor");
        return new BeanPostProcessor() {
            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                System.out.println("加载了bean " + beanName);
                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                return bean;
            }
        };
    }
}

我们可以启动应用发现输出

初始化了 bean BeanPostProcessor
加载了bean org.springframework.context.event.internalEventListenerProcessor
加载了bean org.springframework.context.event.internalEventListenerFactory
加载了bean org.springframework.boot.autoconfigure.AutoConfigurationPackages
加载了bean org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
加载了bean org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
加载了bean objectNamingStrategy
加载了bean mbeanServer
加载了bean mbeanExporter
加载了bean org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
加载了bean spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
加载了bean org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
加载了bean org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration

说明被@Configuration 修饰的类MyConfig本身被扫描了。

  • 方法1:用exclude指明明确要排除的类.
@SpringBootApplication
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {MyConfig.class})})
public class Test  {
    public static void main(String[] args) {
        new SpringApplication(Test.class).run(args);
    }
}

用ComponentScan的excludeFilters属性,可以明确排除调需要扫描的类。

但是这里存在一个问题,如果要排除的类比较多,那这里会看起来很冗余。我们可以采用第二种方式。注解排除。

  • 方法2 : 用注解方式排除
public @interface IgnoreScan {
}

新建此注解。
在需要忽略的类上添加:

@Configuration
@IgnoreScan
public class MyConfig {

    @Bean
    public BeanPostProcessor beanPostProcessor() {
        System.out.println("初始化了 bean BeanPostProcessor");
        return new BeanPostProcessor() {
            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                System.out.println("加载了bean " + beanName);
                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                return bean;
            }
        };
    }
}

这样即可排除掉不被扫描了。

  • 方法3 :
    第三种方式实现org.springframework.core.type.filter.TypeFilter,然后也是ComponentScan去排除指定的注解。网上写的好的文章很多,这里不多说了。

个人觉得这样的需求还是很适合集成在框架里去的。
看了此篇文章是不是感觉收获蛮大

  • 11
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值