@ComponentScan中useDefaultFilters属性误解

事件还原

demo结构

  • com
    • cap2
      • config
        • Cap2MainConfig.java
      • controller
        • OrderController.java
      • dao
        • OrderDao.java
      • server
        • OrderService.java

在这里插入图片描述

代码

主要是测试bean是否被扫描(里面的代码忽略)

@Controller
public class OrderController {
}

@Repository
public class OrderDao {
}

@Service
public class OrderService {
}

核心配置类

import com.cap1.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;

/**
 * ComponentScan 扫描组件
 * value 包扫描的路径
 * excludeFilters = Filter[] 指定扫描的时候按照什么规则排除那些组件
 * includeFilters = Filter[] 指定扫描的时候只需要包含哪些组件
 * type = FilterType.ANNOTATION 按照注解
 * classes = {Service.class} 注解里面的Service
 * useDefaultFilters = false 使用自定义扫描范围要改成false
 * 只扫描我com.cap2下的带有@Service注解的类
 */

@Configuration//配置类负责统一加载bean
@ComponentScan(value = "com.cap2",includeFilters= {@ComponentScan.Filter(
        type = FilterType.ANNOTATION,classes = {Service.class}
)},useDefaultFilters = false)
//ComponentScan扫描组件
public class Cap2MainConfig {
}

结果

@Test
    public void test3() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap2MainConfig.class);
        String[] name = app.getBeanDefinitionNames();
        for (String s : name) {
            System.out.println(s);
        }
    }

在这里插入图片描述
这样的结果是我们应该看到的

问题

于是我就在想我是不是可以搞一个反的test()试一试
刚刚的结果:只扫描我com.cap2下的带有@Service注解的类
突发奇想:在com.cap2下排除带有@Service注解的类

说干就干

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;


@Configuration
@ComponentScan(value = "com.cap2",excludeFilters = {@ComponentScan.Filter(
        type = FilterType.ANNOTATION,classes = {Service.class}
)},useDefaultFilters = false)
public class Cap2MainConfig {
}

测试类省略

在这里插入图片描述
按照我预先设想的应该会有:
orderController
orderDao

为什么会没有呢?

解决方案

查看源码

在这里插入图片描述
useDefaultFilters默认是true,扫描带有@Component ro @Repository ro @Service ro @Controller 的组件
其中@Repository @Service @Controller都实现了@Component

在这里插入图片描述
在这里插入图片描述
因为它们都有实现@Component所以在useDefaultFilters为false时,IOC容器不会去注入带有@Componen的类,它会实现我们自己定义的过滤规则,下面见源码
在这里插入图片描述
因为不会去加载,所以容器里面就没有Bean,当然因为没有所以它也不会去排除(脸黑)
在这里插入图片描述

总结

现在想想,我当时为什么会有这样的想法,哈哈哈哈
我以为第一个demo是通过加载排除(其实是直接找需要的,不通过全部加载然后排除)
误以为第二个也和第一个一样
结果它里面根本就没有bean,也排除不了!!!

### 如何在 Spring Boot 中使用 `@ComponentScan` 的 `excludeFilters` 排除组件 为了防止某些包下的组件被自动扫描并注册到 Spring 容器中,可以利用 `@ComponentScan` 注解中的 `excludeFilters` 属性。这允许指定哪些类型的组件应该被排除在外。 当希望排除特定路径下所有的服务层组件时,可以通过正则表达式来实现这一目标[^1]: ```java @Configuration @ComponentScan( basePackages = "com.example", excludeFilters = @ComponentScan.Filter( type = FilterType.REGEX, pattern = "com\\.example\\.service\\..*" ) ) public class AppConfig { // 配置类定义 } ``` 这段代码展示了如何配置应用程序上下文只扫描 `com.example` 包及其子包内的组件,但不包括任何位于 `com.example.service.*` 下的服务类。 如果需要更复杂的过滤逻辑,比如基于自定义注解或其他条件,则可调整 `type` 参数以及相应的匹配模式。例如,要依据注解类型进行筛选,可以选择 `FilterType.ANNOTATION` 并提供具体的注解类作为参数。 对于多个不同的排除规则,可以在 `excludeFilters` 上应用数组形式传递多个 `Filter` 实例。 ```java @Configuration @ComponentScan( basePackages = "com.example", excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = MyService.class), @ComponentScan.Filter(type = FilterType.CUSTOM, classes = CustomExcludeFilter.class) } ) public class AppConfig { // 配置类定义 } ``` 此示例说明了同时运用多种方式(如按具体类型或通过自定义过滤器)来进行更为精细的控制。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值