@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. boltdb 原理

    简介 介绍及简单使用:https://www.cnblogs.com/daemon365/p/17690167.html 源码地址:https://github.com/etcd-io/bbolt p ...

  2. 正式开启全站HTTPS加密之旅

    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 正式开启全站HTTPS加密之旅 日期:2017-7-14 ...

  3. 让matplotlib在绘图时显示中文

    让matplotlib绘图时显示中文. 安装中文字体 apt install fonts-wqy-microhei 清除matplotlib的缓存    rm -rf ~/.cache/matplot ...

  4. 高通安卓:自定义QFile烧录镜像

    高通安卓:自定义QFile烧录镜像 背景 在某个项目中,因为USB口的问题,无法使用fastboot进行download. 同事提供了一份用与QFile的rawprogram.xml烧写.觉得这个方法 ...

  5. 高通平台mm-camera上电时序

    高通平台mm-camera上电时序 背景 作为高通平台Camera知识的一种补充. 参考文档:https://blog.csdn.net/m0_37166404/article/details/649 ...

  6. QT学习:00 介绍

    --- title: framework-cpp-qt-00-介绍 date: 2020-04-08 15:41:54 categories: tags: - c/c++ - qt --- 章节描述: ...

  7. Java进阶:HashMap底层原理(通俗易懂篇)

    1.底层结构 Java 7及之前版本 在Java 7及之前的版本中,HashMap的底层数据结构主要是数组加链表.具体实现如下: 数组:HashMap的核心是一个Entry数组(Entry<K, ...

  8. ajax过程?

    1. 创建ajax对象var xhr = new XMLHttpRequest(); 2.告诉Ajax对象要向哪发送请求,以什么方式发送       //请求方式 请求地址xhr.open('get' ...

  9. 拆解LangChain的大模型记忆方案

    之前我们聊过如何使用LangChain给LLM(大模型)装上记忆,里面提到对话链ConversationChain和MessagesPlaceholder,可以简化安装记忆的流程.下文来拆解基于Lan ...

  10. (五)Redis 缓存异常、应对策略

    1.缓存和数据库不一致 只要我们使用 Redis 缓存,就必然会面对缓存和数据库间的一致性保证问题,这里的"一致性"包含了两种情况:缓存中有数据且与数据库中的值相同.缓存中没有数据 ...