@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, RestController.class}))

前言

  • springboot 2.0.0.RELEASE
  • maven 3.5.0

自动扫描

默认扫描 @SpringBootApplication 类所在目录及子目录。

跳过扫描

@ComponentScan 搭配 excludeFilters

示例:

@ComponentScan(excludeFilters = @Filter(FilterType.ANNOTATION, classes = Controller.class))
  • 1

参考:https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/api/index.html?org/springframework/boot/context/TypeExcludeFilter.html

FilterType

public enum FilterType {

	/**
* Filter candidates marked with a given annotation.
* @see org.springframework.core.type.filter.AnnotationTypeFilter
*/
ANNOTATION, /**
* Filter candidates assignable to a given type.
* @see org.springframework.core.type.filter.AssignableTypeFilter
*/
ASSIGNABLE_TYPE, /**
* Filter candidates matching a given AspectJ type pattern expression.
* @see org.springframework.core.type.filter.AspectJTypeFilter
*/
ASPECTJ, /**
* Filter candidates matching a given regex pattern.
* @see org.springframework.core.type.filter.RegexPatternTypeFilter
*/
REGEX, /** Filter candidates using a given custom
* {@link org.springframework.core.type.filter.TypeFilter} implementation.
*/
CUSTOM }

FilterType.ANNOTATION 按注解类型排除

@SpringBootApplication
@ComponentScan(excludeFilters = @Filter(FilterType.ANNOTATION, classes = Controller.class))
public class Application { public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
} @Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("startup done.");
};
}
}

FilterType.REGEX 正则表示达

@SpringBootApplication
@ComponentScan(excludeFilters = @Filter(type = FilterType.REGEX, pattern = {"net.sayyy.db.*"}))
public class Application { public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
} @Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("startup done.");
};
}
}

FilterType.CUSTOM 自定义规则

@SpringBootApplication
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = MyCustomerFilter.class))
public class Application { public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
} @Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("startup done.");
};
}
}

自定义过滤规则是需要实现 TypeFilter 接口
自定义扫描规则, 匹配类名中带有 Service 的类

public class MyCustomerFilter implements TypeFilter {

    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
String className = metadataReader.getClassMetadata().getClassName();
return className.contains("Service");
}
}

【sprinb-boot】@ComponentScan 跳过扫描 excludeFilters的更多相关文章

  1. SpringMVC 源码深度解析<context:component-scan>(扫描和注冊的注解Bean)

    我们在SpringMVC开发项目中,有的用注解和XML配置Bean,这两种都各有自己的优势,数据源配置比較经经常使用XML配置.控制层依赖的service比較经经常使用注解等(在部署时比較不会改变的) ...

  2. spring boot controller路由 url 扫描不到问题

    spring boot项目出现controller的路由没被注册,原因:启动类application跟controller不在一个包中,扫描不到controller, 如启动类在com.oyx.a,c ...

  3. context:component-scan 注解的扫描

    <context:component-scan base-package="com.matt.cloud"/> bean-context中 spring.handler ...

  4. Spring Boot项目跳转不到jsp页面是怎么回事

    SpringBoot访问不了JSP但却能进入后台 一直报错: 解决方法: 改成下面的

  5. 【Spring源码分析系列】启动component-scan类扫描加载过程

    原文地址:http://blog.csdn.net/xieyuooo/article/details/9089441/ 在spring 3.0以上大家都一般会配置一个Servelet,如下所示: &l ...

  6. spring启动component-scan类扫描加载过程(转)

    文章转自 http://www.it165.net/pro/html/201406/15205.html 有朋友最近问到了 spring 加载类的过程,尤其是基于 annotation 注解的加载过程 ...

  7. 精尽Spring Boot源码分析 - 剖析 @SpringBootApplication 注解

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  8. 【springboot】给你一份Spring Boot知识清单

    目录: 一.抛砖引玉:探索Spring IoC容器 1.1.Spring IoC容器 1.2.Spring容器扩展机制 二.夯实基础:JavaConfig与常见Annotation 2.1.JavaC ...

  9. Spring Boot 自动扫描组件

    使用@ComponentScan自动扫描组件 案例准备 1.创建一个配置类,在配置类上添加 @ComponentScan 注解.该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <con ...

  10. spring boot(二):启动原理解析

    我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...

随机推荐

  1. Java日期时间API系列25-----Jdk8中java.time包中的新的日期时间API类,使用MonthDay计算十二星座。

    通过Java日期时间API系列24-----Jdk8中java.time包中的新的日期时间API类,MonthDay类源码和应用,对比相同月日时间.对MonthDay简单做了说明和应用.十二星座是根据 ...

  2. 2020年度国产数据库:openGauss

    根据墨天轮2020年一年的数据库流行度得分趋势变化,我们选出了流行热度增长显著的数据库为2020年度国产数据库.恭喜 华为开源关系型数据库 openGauss 荣获 "2020年度国产数据库 ...

  3. 利用 Kubernetes 内置 PodTemplate 管理 Jenkins 构建节点

    作者:Rick Jenkins 可以很好地与 Kubernetes 集成,不管是控制器(controller)还是构建节点(agent),都能以 Pod 的形式运行在 Kubernetes 上. 熟悉 ...

  4. Java高并发Lock接口讲解,精准通知线程间的执行顺序

    题目:两个线程操作一个变量,实现两个线程对同一个资源一个进行加1操作,另外一个进行减1操作,且需要交替实现,变量的初始值为0.即两个线程对同一个资源进行加一减一交替操作. Lock接口与Conditi ...

  5. 洛谷 P1540 [NOIP2010 提高组] 机器翻译

    题目概括 给定 N 个整数,和一个容量为 M 的"字典",从头到尾依次翻译,每次翻译先看自家字典,没有的话再看别人的字典并存到自家字典,如果自家字典满了,当前单词的翻译会代替最早进 ...

  6. TRLO: An Efficient LiDAR Odometry with 3D Dynamic Object Tracking and Removal

    arxiv | 中科院联合国科大开源 TRLO:一种结合3D动态物体跟踪与移除的高效LiDAR里程计 [TRLO: An Efficient LiDAR Odometry with 3D Dynami ...

  7. 记录java接口自动化模板优化

    项目路径说明 内容优化 优化内容 1.自动生成的测试报告集成至项目中,可直接通过项目访问测试报告(之前生成测试报告位于项目外,需要手动打开) 优化效果: 2.后续会增加allure测试报告集成使用(实 ...

  8. Cursor使用

    Cursor是一款AI 代码编辑器,官网地址为https://www.cursor.com/,直接在官网下载安装即可,基于VS Code二次开发而来,之所以没有采用插件方式,在官方网站上给出的答案是某 ...

  9. 【一步步开发AI运动小程序】十七、如何识别用户上传视频中的人体、运动、动作、姿态?

    [云智AI运动识别小程序插件],可以为您的小程序,赋于人体检测识别.运动检测识别.姿态识别检测AI能力.本地原生识别引擎,内置10余个运动,无需依赖任何后台或第三方服务,有着识别速度快.体验佳.扩展性 ...

  10. tsconfig配置详解

    { "compilerOptions": { /* Basic Options */ "target": "es5" /* target用于 ...