@ComponentScan
@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的更多相关文章
- spring源码分析之<context:component-scan/>vs<annotation-config/>
1.<context:annotation-config/> xsd中说明: <xsd:element name="annotation-config"> ...
- context:component-scan" 的前缀 "context" 未绑定。
SpElUtilTest.testSpELLiteralExpressiontestSpELLiteralExpression(cn.zr.spring.spel.SpElUtilTest)org.s ...
- 为什么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 ...
- context:component-scan标签的use-default-filters属性的作用以及原理分析
一.背景 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,那么本文就来分析下出现这种问题可能的原因以及解决方式. 二.分析及原理窥探 1.项目结构 ...
- spring <context:component-scan>使用说明(转)
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...
- [转载]Difference between <context:annotation-config> vs <context:component-scan>
在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...
- <context:component-scan>使用说明
Spring组件扫描<context:component-scan/>使用详解 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫 ...
- 关于context:component-scan配置中use-default-filters参数的作用
参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下: <context:component-scan base-package=&q ...
- annotation-config vs component-scan – Spring Core--转
原文地址:http://techidiocy.com/annotation-config-vs-component-scan-spring-core/ <context:annotation-c ...
- context:component-scan扫描使用的use-default-filters
如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean. <context:component-scan base-package ...
随机推荐
- CentOS7学习笔记(四) 系统运行级别
什么是运行级别 在CentOS系统中包含七种运行级别,例如命令行或图形化界面就是最常用的运行级别 运行级别的两种表示方式及作用 运行级别 运行级别 作用说明 0 poweroff.target 关机 ...
- 【题解】CatOJ C0458C 滑动窗口定期重构
标题 trick 的名字我也不知道是什么,就这样吧. 首先有显然的 dp 式子:\(f(i)=\min \{f(j) \times \max\{a_{j+1},\dots,a_i\}\}\).考虑怎么 ...
- Ubuntu中安装最新 Node.js 和 npm
背景 为了安装GNU QEMU Eclipse,需要安装比较新的nodejs $ npm install --global xpm@latest ▌ ╢░░░░░░░░░░░░░░░░░░░░░░░░ ...
- manim边学边做--MathTex
上一篇介绍的SingleStringMathTex主要用来显示只有一行的数学公式,对于复杂的数学公式,可以使用MathTex类. MathTex类继承自SingleStringMathTex,在其基础 ...
- 使用VS Code 学习算法(第四版)
最近在学习算法(第四版),书中一直在使用命令行来执行Java程序,而使用Eclipse时,很难使用命令行,或者说我根本就不会用,于是就想研究一下使用VS Code来编写代码,使用命令行来执行程序.看了 ...
- ISCTF2023
ISCTF 2023 Misc 签到题 公众号发送:小蓝鲨,我想打ctf ISCTF{W3lcom3_7O_2023ISCTF&BlueShark} 你说爱我?尊嘟假嘟 你说爱我替换.,真嘟替 ...
- VSCode如何设置Vue前端的debug调试
vscode在调试vue.代码时,如何进行debug? 1.安装Chrome Debug插件. 2.在launch.json中,将url修改成你前端项目的路径: 1 { 2 // Use Intell ...
- java将list中某个元素放在首位
java将list中某个元素放在首位 1 List<Example> example = exampleRepository.list(); 2 3 //将list里的某个字符串默认排列在 ...
- Solo 开发者周刊 (第 1 期):开源产品的探索之路
产品推荐 如何着手将一个简单的想法转变为一个成熟的开源项目,以及如何在此过程中利用和贡献于开源社区.同时使其达到商业化的同时,保持原有的开源精神.这些是我们需要探索的. Spug 开源运维平台 Spu ...
- Windows 10 LTSC启用Microsoft Store的方法
新建msreg.bat文件,并编辑内容如下: ========== @echo off :: BatchGotAdmin :------------------------------------- ...