@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. CSharp的lambda表达式的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...

  2. AI之道|诺奖对AI的偏爱是真魔幻【悟空非空也】

    一.背景 回归 2024 年诺贝尔物理学奖被授予 John J.Hopfield(霍普菲尔德) 和 Geoffrey E.Hinton(辛顿),当时物理学界都震惊了,纷纷在打听霍普菲尔德和辛顿,他们两 ...

  3. Linux+PXE+DHCP+TFTP+NFS实现无人值守安装

    一.实验环境:OS:Redhat6.4软件:DHCP服务.TFTP服务.NFS服务硬件:Dell R630服务器两台物理连接图: em1 em1 二.工作原理:服务器通过PXE网卡启动,从dhcp服务 ...

  4. 云原生周刊:Kubernetes v1.31 中的移除和主要变更|2024.7.22

    开源项目 Argo Rollouts Argo Rollouts 是一个 Kubernetes 控制器和一组自定义资源定义(CRDs),提供高级部署功能,例如蓝绿部署.金丝雀部署.金丝雀分析.实验以及 ...

  5. 云原生周刊:Helm Charts 深入探究 | 2024.3.11

    开源项目推荐 Glasskube Glasskube 提供了一个用于 Kubernetes 的缺失的包管理器.它具有图形用户界面(GUI)和命令行界面(CLI).Glasskube 包是具备依赖感知. ...

  6. 选择程序设计(python)

    文章目录 1.基本概念 2.程序基本结构 3.实例 3.1 判断一个人年龄属于未成年人还是成年人 3.2判断是否是闰年 1.基本概念 Python选择程序设计是通过一条或多条语句的执行结果(True或 ...

  7. 函数(C语言)

    目录 1. 函数的概念 2. 库函数 2.1 标准库和头文件 2.2 库函数的使用方法 3. 自定义函数 3.1 函数的语法形式 3.2 函数的举例 4. 形参和实参 4.1 实参 4.2 形参 4. ...

  8. 4年经验来面试20K的测试岗,连基础都不会,还不如招应届生。

    公司前段时间缺人,也面了不少测试,结果竟然没有一个合适的.一开始瞄准的就是中级的水准,也没指望来大牛,提供的薪资在10-20k,面试的人很多,但平均水平很让人失望.看简历很多都是3.4年工作经验,但面 ...

  9. 4.7 Linux压缩文件或目录中文件为.bz2格式(bzip2命令)

    bzip2 命令同 gzip 命令类似,只能对文件进行压缩(或解压缩),对于目录只能压缩(或解压缩)该目录及子目录下的所有文件.当执行压缩任务完成后,会生成一个以".bz2"为后缀 ...

  10. c++11新增内容

    记录一下c++11新特性方便以后回忆 1.nullptr (对标NULL) 2.auto ,decltype(根据表达式推断类型,表达式不执行) decltype(func()) sum = 5; / ...