文章目录

  • 一、作用
  • 二、注解属性说明
  • 三、使用方式

一、作用

主要是从定义的扫描路径中,找出标识了需要装配的类自动装配到Spring的bean容器中。

简单的说就是 @ComponentScan告诉Spring从哪里找到bean,一旦指定了,Spring就会将指定的包及其下级的包中寻找bean。

在SpringBoot项目中,我们并没有显示的看到该注解,但是仍然能扫描到bean呢?

其实,在创建SpringBoot项目中,默认在启动类上添加了@SpringBootApplication注解,该注解中包含@ComponentScan注解,因此SpringBoot会自动帮我们扫描启动类所在的包。

二、注解属性说明

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {/*** 对应的包扫描路径 可以是单个路径,也可以是扫描的路径数组* @return*/@AliasFor("basePackages")String[] value() default {};/*** 和value一样是对应的包扫描路径 可以是单个路径,也可以是扫描的路径数组* 如果为空,则以@ComponentScan注解的类所在的包为基本的扫描路径* @return*/@AliasFor("value")String[] basePackages() default {};/*** 指定具体的扫描的类* @return*/Class<?>[] basePackageClasses() default {};/*** 对应的bean名称的生成器 默认的是BeanNameGenerator* @return*/Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;/*** 处理检测到的bean的scope范围*/Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;/*** 是否为检测到的组件生成代理* Indicates whether proxies should be generated for detected components, which may be* necessary when using scopes in a proxy-style fashion.* <p>The default is defer to the default behavior of the component scanner used to* execute the actual scan.* <p>Note that setting this attribute overrides any value set for {@link #scopeResolver}.* @see ClassPathBeanDefinitionScanner#setScopedProxyMode(ScopedProxyMode)*/ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;/*** 控制符合组件检测条件的类文件   默认是包扫描下的  **/*.class* @return*/String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN;/*** 是否对带有@Component @Repository @Service @Controller注解的类开启检测,默认是开启的* @return*/boolean useDefaultFilters() default true;/*** 指定某些定义Filter满足条件的组件 FilterType有5种类型如:*          ANNOTATION, 注解类型 默认*          ASSIGNABLE_TYPE,指定固定类*          ASPECTJ, ASPECTJ类型*          REGEX,正则表达式*          CUSTOM,自定义类型* @return*/Filter[] includeFilters() default {};/*** 排除某些过来器扫描到的类* @return*/Filter[] excludeFilters() default {};/*** 扫描到的类是都开启懒加载 ,默认是不开启的* @return*/boolean lazyInit() default false;
}

三、使用方式

启动类

package com.springboottest;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
public class SpringbootTestApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTestApplication.class, args);}
}

启动类在 com.springboottest 包下,那么Spring默认会自动扫描该包及其子包下的bean。

Student类(包路径:com.springtest,非启动类所在的包下):

package com.springtest;import lombok.Data;
import org.springframework.stereotype.Component;@Data
@Component
public class Student {private String name;private String nickName;
}

如果想让Student类被Spring扫描到,那么我们可在启动类中增加如下注解:

package com.springboottest;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
@ComponentScan(basePackages = {"com.springtest", "com.springboottest"})
// @ComponentScan(basePackages = {"com"})
public class SpringbootTestApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTestApplication.class, args);}
}

上面的代码我们可以看到,@ComponentScan注解中设置了两个包名("com.springtest", "com.springboottest"),这样设置是为了精确扫描范围,当然我们也可以使用两个包名的共同前缀com

注:如果需要扫描的类在不同的包下,最好是精确指定要扫描的包,这样可以减少加载时间。

SpringBoot —— @ComponentScan注解相关推荐

  1. SpringBoot—@ComponentScan注解过滤排除某个类

    关注微信公众号:CodingTechWork,一起学习进步. 问题   在抽取公共swagger配置类时,将swagger放入com.test.common.config包内,其他模块通过@Compo ...

  2. Springboot中@ComponentScan 注解

    三个点: 1.工程中Application类的位置.默认情况下就不需要配置@ComponentScan这个注解了. 因为Application类,在启动的时候,默认是加载和Application类所在 ...

  3. 二.Springboot 常用注解

    @SpringBootApplication: 包含@Configuration.@EnableAutoConfiguration.@ComponentScan通常用在主类上. 很多SpringBoo ...

  4. SpringBoot核心注解介绍

    我们看一下SpringBoot核心注解的一个介绍,其实我们之前在SpringBoot当中呢,我们用过这些注解了,只是我们没有去说一下每个注解的详细含义,那么我们在这里把它补齐,我们打开我们的代码,我们 ...

  5. Spring/SpringBoot常用注解总结

    目录如下,内容有点多: 1. @SpringBootApplication 这里先单独拎出@SpringBootApplication 注解说一下,虽然我们一般不会主动去使用它. 注:这个注解是 Sp ...

  6. 近100个Spring/SpringBoot常用注解汇总!

    作者 | Guide 来源 | JavaGuide(微信公众号) 毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我 ...

  7. SpringBoot核心注解@SpringBootApplication一二

    SpringBoot核心注解@SpringBootApplication,用于SpringBoot项目的启动类上,在 2.2.0.RELEASE 版本中是4个注解的组合,即 @SpringBootCo ...

  8. SpringBoot2.1.5 (4)---SpringBoot 常用注解说明

    SpringBoot2.1.5 (4)---SpringBoot 常用注解说明 @SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入 ...

  9. SpringBoot常用注解说明

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

最新文章

  1. mysql事务和锁InnoDB(转)
  2. Activiti源码之建造者模式 Builder
  3. 人工智能的概念和知识构架_概念验证:玩! 构架
  4. 人工智能AI实战100讲(二)-自动驾驶传感器之激光雷达(三)主流车厂激光雷达的选择及布局
  5. 新一代云原生监控系统Prometheus--理解数据模型/指标/标签/PromQL/Exporter
  6. BCELoss忽视某个类别
  7. mongooseDB数据库添加账号
  8. java两个和三个_H2DB和Java,大约两个小时的差异
  9. 蚂蚁自研数据库OceanBase基于木兰公共协议正式开源
  10. 字幕/打轴/压制小tip——Aegisub MeGUI 极简操作指南
  11. 一日一技:Python + Excel——飞速处理数据分析与处理
  12. 热血格斗场(二分法+STL运用)
  13. c语言程序运行超时是怎么回事,这个运行超时是什么原因?求助~
  14. STM32定时 计算公式
  15. 群晖emby服务端下载(弃坑,官网已经能顺畅访问)
  16. Python绘制双对数曲线
  17. MFC基本图形的绘制(一)设备环境类CDC、画笔和画刷
  18. [总结]蓝牙各个版本的关系和区别
  19. Python通过图片识别实现连续点击
  20. 关闭windows正版验证

热门文章

  1. python 曲面_Python之OpenGL笔记(35):曲面物体的构建
  2. h5链接加上 vconsole_又出爆款!凯美瑞和红旗H5没法比!
  3. 【转】产品经理如何进行BRD,MRD,PRD,DRD,FRD编写
  4. IIS/ASP.NET 管道
  5. [你必须知道的.NET]第二十六回:认识元数据和IL(下)
  6. 【转】SPSite、SPWeb对象模型(转winos.cn)
  7. python 语句简写_自学Python-语句之列表推导式
  8. Hbase2修复 - HBCK2
  9. spark 算子例子_Spark性能调优方法
  10. 【2019牛客暑期多校训练营(第三场)- F】Planting Trees(单调队列,尺取)