@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. 28. 找出字符串中第一个匹配项的下标 Golang实现

    题目描述: 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始).如果 needle 不是 hay ...

  2. 云原生周刊:Kubernetes 1.29 中的删除、弃用和主要更改 | 2023.11.27

    开源项目推荐 Orphaned ConfigMaps 该版本库包含一个脚本,用于识别 Kubernetes 命名空间中的孤立的配置映射.孤立的配置映射是指那些未被命名空间中的任何活动 Pod 或容器引 ...

  3. Linux+Nginx+Php+MariaDB+Redis部署

    目录 工作机制 系统环境描述 部署Nginx 安装 启动 测试并访问 部署PHP 安装 启动 配置Nginx 测试 部署MariaDB 安装 启动 配置php支持 测试 部署Redis 安装 启动 配 ...

  4. RocketMQ 在小米的多场景灾备实践案例

    本文作者:邓志文.王帆 01为什么要容灾? 在小米内部,我们使用 RocketMQ 来为各种在线业务提供消息队列服务,比如商城订单.短信通知甚至用来收集 IoT 设备的上报数据,可以说 RocketM ...

  5. 【小 w 的代数】(提供一种 n^2 log 的解法)

    前言: 卖点 记录 CTH 的发言 CTH:你这真是 n^3 的 CTH:我也不知道你线段树优化个啥,\(n^3 \log n\) CTH:你优化到哪了啊 CTH:······你从赛时打这个题到现在 ...

  6. Wgpu图文详解(02)渲染管线与着色器

    在本系列的第一篇文章中(<Wgpu图文详解(01)窗口与基本渲染>),我们介绍了如何基于0.30+版本的winit搭建Wgpu的桌面环境,同时也讲解了关于Wgpu一些基本的概念.模块以及架 ...

  7. C#中的9个“黑魔法”

    C#中的9个"黑魔法"与"骚操作" 我们知道C#是非常先进的语言,因为是它很有远见的"语法糖".这些"语法糖"有时过于好 ...

  8. 不错的PHP扩展

    不错的PHP扩展 ext name ext description ds data structure 提供list hash queue等数据结构 igbinary 数据压缩(速度快 压缩后内容小) ...

  9. 关于开启了auth的站点如何使用curl访问

    有的站点开了 auth 如何访问呢 可以直接这样 将 用户名密码放入URL中 http://username:password@host:8080/index.html 即可.

  10. 全网最适合入门的面向对象编程教程:59 Python并行与并发-并行与并发和线程与进程

    全网最适合入门的面向对象编程教程:59 Python 并行与并发-并行与并发和线程与进程 摘要: 在 Python 中,"并行"(parallelism)与"并发&quo ...