SpringBoot - 配置包扫描注解@ComponentScan

@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);
    }
}
复制代码

 

posted on   Mikasa-Ackerman  阅读(2950)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示