首页 > 其他分享 >SpringBoot - 配置包扫描注解@ComponentScan

SpringBoot - 配置包扫描注解@ComponentScan

时间:2022-12-20 16:11:06浏览次数:40  
标签:SpringBoot FilterType ComponentScan 过滤 注解 classes type class

@ComponentScan

作用:配置包扫描规则

范围:主程序类上(被@SpringBootApplication修饰),或配置类上(被@Configuration修饰)

参数:value 指定要扫描的包,excludeFilters 配置排除规则,includeFilters 配包含规则

注意:要设置useDefaultFilters = false(系统默认为true,需要手动设置) includeFilters包含过滤规则才会生效。

 

@Filter

作用:配合@ComponentScan 使用,excludeFilters或includeFilters

参数:

type参数的值

FilterType.ANNOTATION:按照注解过滤

FilterType.ASSIGNABLE_TYPE:按照给定的类型过滤

FilterType.ASPECTJ:按照ASPECTJ表达式过滤

FilterType.REGEX:按照正则表达式过滤

FilterType.CUSTOM:按照自定义规则过滤

classes参数的值

如type参数为FilterType.ANNOTATION:按照注解过滤,classes的值可以为{Controller.class,Service.class}

如type参数为FilterType.ASSIGNABLE_TYPE:按照给定的类型过滤,classes的值可以为{类.class,自定义类.class}

 

基本使用:

@SpringBootApplication
//定义包扫描规则
@ComponentScan(
        value = "com.example.learn20221218.bean",
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})},
        includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {Object.class})},
        useDefaultFilters = false
)
public class Learn20221218Application {
    public static void main(String[] args) {
        SpringApplication.run(Learn20221218Application.class, args);
    }
}

 

标签:SpringBoot,FilterType,ComponentScan,过滤,注解,classes,type,class
From: https://www.cnblogs.com/ErenYeager/p/16994414.html

相关文章