@ComponentScan 是一个注解,用于Spring框架,它允许开发者指定Spring应该扫描哪个包或包下的子包来寻找组件(如@Component@Service@Repository等注解标注的类)。通过使用 @ComponentScan,开发者可以自动化地注册这些组件,使得它们能够被Spring容器管理。

语法

@ComponentScan 注解的基本语法如下:

@ComponentScan(basePackages = "com.example", useDefaultFilters = false, includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {MyAnnotation.class})}, excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {MyAnnotation.class})})

属性

  • basePackages:用于指定Spring应该扫描的包。这个属性可以接受一个字符串数组,也可以接受一个点号分隔的包路径。
  • useDefaultFilters:如果设置为true,则使用默认的过滤器,即扫描所有带有@Component@Service@Repository@Controller注解的类。如果设置为false,则不会使用默认的过滤器。
  • includeFilters:用于指定Spring应该包含哪些类型的类。这个属性可以接受一个过滤器数组,每个过滤器都包含一个类型和一个类。例如,@Filter(type = FilterType.ANNOTATION, classes = {MyAnnotation.class}) 表示只扫描带有MyAnnotation注解的类。
  • excludeFilters:用于指定Spring不应该包含哪些类型的类。这个属性和includeFilters类似,也可以接受一个过滤器数组。

示例

以下是一个使用 @ComponentScan 的示例:

@SpringBootApplication
@ComponentScan(basePackages = "com.example", useDefaultFilters = false, includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {MyAnnotation.class})}, excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {MyAnnotation.class})})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

在这个示例中,Spring将扫描com.example包及其子包,只包含带有MyAnnotation注解的类,并排除带有MyAnnotation注解的类。

应用场景

  • 自动化组件注册:通过 @ComponentScan,Spring可以自动扫描并注册带有特定注解的类,从而简化了组件注册的过程。
  • 微服务架构:在微服务架构中,每个服务可能会有多个模块,每个模块都有自己的一套组件。使用 @ComponentScan 可以方便地在每个模块中定义自己的扫描路径,从而实现模块化开发。

注意事项

  • @ComponentScan 注解通常用于Spring Boot应用的主类上,这样Spring Boot会自动扫描该类所在的包及其子包。
  • 如果需要对特定的模块或包进行扫描,可以将 @ComponentScan 注解放在该模块或包的入口类上。
  • 如果需要排除某些包或类,可以通过设置excludeFilters属性来实现。

    总之,@ComponentScan 是Spring框架中的一个重要注解,它允许开发者自动化地注册带有特定注解的类,从而简化组件注册的过程。通过合理地使用这个注解,可以实现模块化开发和微服务架构。

@ComponentScan的更多相关文章

  1. spring源码分析之<context:component-scan/>vs<annotation-config/>

    1.<context:annotation-config/> xsd中说明: <xsd:element name="annotation-config"> ...

  2. context:component-scan" 的前缀 "context" 未绑定。

    SpElUtilTest.testSpELLiteralExpressiontestSpELLiteralExpression(cn.zr.spring.spel.SpElUtilTest)org.s ...

  3. 为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />

    在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤. <context:component-scan base-packa ...

  4. context:component-scan标签的use-default-filters属性的作用以及原理分析

    一.背景 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,那么本文就来分析下出现这种问题可能的原因以及解决方式. 二.分析及原理窥探 1.项目结构 ...

  5. spring <context:component-scan>使用说明(转)

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...

  6. [转载]Difference between <context:annotation-config> vs <context:component-scan>

    在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...

  7. <context:component-scan>使用说明

    Spring组件扫描<context:component-scan/>使用详解 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫 ...

  8. 关于context:component-scan配置中use-default-filters参数的作用

    参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下: <context:component-scan base-package=&q ...

  9. annotation-config vs component-scan – Spring Core--转

    原文地址:http://techidiocy.com/annotation-config-vs-component-scan-spring-core/ <context:annotation-c ...

  10. context:component-scan扫描使用的use-default-filters

    如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean. <context:component-scan base-package ...

随机推荐

  1. SpringBoot指标监控功能

    SpringBoot指标监控功能 随时查看SpringBoot运行状态,将状态以josn格式返回 添加Actuator功能 Spring Boot Actuator可以帮助程序员监控和管理Spring ...

  2. 大数据面试吹牛草稿V2.0

    面试吹牛之前先打个草稿! 各位面试官好! 我叫 xxx,毕业于 xxx,之前在 xxx 公司待了 1 年多,期间⼀直从事的是 IT 行业,刚开始的时候做的是 Java 开发后来转岗到大数据方向做大数据 ...

  3. AtCoder Beginner Contest 302 H. Ball Collector 题解 可撤销并查集

    为了更好的阅读体验,请单击这里 AtCoder Beginner Contest 302 H. Ball Collector 题意跳过. 可以视作将 \(a_i, b_i\) 之间连了一条边,然后 \ ...

  4. Python中使用MySQL模糊查询的方法

    1.方法一:使用pymysql库的方法 当在Python中使用MySQL进行模糊查询时,我们通常会使用pymysql或mysql-connector-python这样的库来连接MySQL数据库并执行查 ...

  5. Xilinx SDK 开发Linux APP

    Xilinx SDK 开发Linux APP 步骤 配置环境变量 将工具链需要的程序的所在目录添加到 系统环境变量中,例如: D:\Xilinx_201803\SDK\2018.3\gnu\micro ...

  6. 模拟用户登录-cookes

    import requests url = 'https://www.xread8.com/user/login.json' headers = { 'User-Agent': 'Mozilla/5. ...

  7. P8571 题解

    既然字符串的总长一定,不妨对于每个询问中的 \(s_k\) 的长度根号分治,假定分治阈值为 \(B\).下面令 \(L\) 为所有串长度总和. 对于长度大于 \(B\) 的字符串,这样的不同字符串至多 ...

  8. 韦东山freeRTOS系列教程之【第六章】信号量(semaphore)

    目录 系列教程总目录 概述 6.1 信号量的特性 6.1.1 信号量的常规操作 6.1.2 信号量跟队列的对比 6.1.3 两种信号量的对比 6.2 信号量函数 6.2.1 创建 6.2.2 删除 6 ...

  9. Java BigDecimal 算术运算

    算术运算 BigDecimal bignum1 = new BigDecimal("10"); BigDecimal bignum2 = new BigDecimal(" ...

  10. WPF单行TextBox自动滚动至末尾

    根据光标位置自动滚动 textBox.CaretIndex = textBox.SelectionStart; var rect = textBox.GetRectFromCharacterIndex ...