在配置类上添加 @ComponentScan 注解。该注解默认会扫描该类所在的包下所有的配置类,相当于xml的 <context:component-scan>。

@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中。

新建三个类

  1. @Controller
  2. public class OrderController {
  3. }
  4. @Service
  5. public class OrderService {
  6. }
  7. @Repository
  8. public class OrderDao {
  9.  
  10. }

配置类

  1. @Configuration
  2. @ComponentScan("cn.qin.test") //扫描该路径下的
  3. public class Config {
  4. }

查看容器中的Bean

  1. AnnotationConfigApplicationContext app=new AnnotationConfigApplicationContext(Config.Class);
  2. String[] names=app.getBeanDefinitionNames();
  3. for (String name : names){
  4. System.out.println(name);
  5. }

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
Config
orderController
orderDao
orderService

  1.  

源码分析

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.TYPE)
  3. @Documented
  4. @Repeatable(ComponentScans.class)
  5. public @interface ComponentScan {
  6. /**
  7. * 对应的包扫描路径 可以是单个路径,也可以是扫描的路径数组
  8. * @return
  9. */
  10. @AliasFor("basePackages")
  11. String[] value() default {};
  12. /**
  13. * 和value一样是对应的包扫描路径 可以是单个路径,也可以是扫描的路径数组
  14. * @return
  15. */
  16. @AliasFor("value")
  17. String[] basePackages() default {};
  18. /**
  19. * 指定具体的扫描的类
  20. * @return
  21. */
  22. Class<?>[] basePackageClasses() default {};
  23. /**
  24. * 对应的bean名称的生成器 默认的是BeanNameGenerator
  25. * @return
  26. */
  27. Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
  28. /**
  29. * 处理检测到的bean的scope范围
  30. */
  31. Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
  32. /**
  33. * 是否为检测到的组件生成代理
  34. * Indicates whether proxies should be generated for detected components, which may be
  35. * necessary when using scopes in a proxy-style fashion.
  36. * <p>The default is defer to the default behavior of the component scanner used to
  37. * execute the actual scan.
  38. * <p>Note that setting this attribute overrides any value set for {@link #scopeResolver}.
  39. * @see ClassPathBeanDefinitionScanner#setScopedProxyMode(ScopedProxyMode)
  40. */
  41. ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
  42. /**
  43. * 控制符合组件检测条件的类文件 默认是包扫描下的 **/*.class
  44. * @return
  45. */
  46. String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN;
  47. /**
  48. * 是否对带有@Component @Repository @Service @Controller注解的类开启检测,默认是开启的
  49. * @return
  50. */
  51. boolean useDefaultFilters() default true;
  52. /**
  53. * 指定某些定义Filter满足条件的组件 FilterType有5种类型如:
  54. * ANNOTATION, 注解类型 默认
  55. ASSIGNABLE_TYPE,指定固定类
  56. ASPECTJ, ASPECTJ类型
  57. REGEX,正则表达式
  58. CUSTOM,自定义类型
  59. * @return
  60. */
  61. Filter[] includeFilters() default {};
  62. /**
  63. * 排除某些过来器扫描到的类
  64. * @return
  65. */
  66. Filter[] excludeFilters() default {};
  67. /**
  68. * 扫描到的类是都开启懒加载 ,默认是不开启的
  69. * @return
  70. */
  71. boolean lazyInit() default false;
  72. }
  1. includeFilters 包含
    excludeFilters 排除
  1. @Configuration
  2. @ComponentScan(value = "cn.qin.test",includeFilters = {
  3. @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}), // 注解类型 Controller
  4. @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {OrderService.class}), //固定类型 是OrderService
  5. },useDefaultFilters = false)
  6. public class Config {
  7. }
  1. 一定要设置useDefaultFilters =false 不然无效
  1.  
    自定义 Filter
  1. public class CustomFilter implements TypeFilter {
  2. /**
  3. *
  4. * @param metadataReader 读取当前正在扫描类的信息
  5. * @param metadataReaderFactory 可以获取到其他任何类的信息
  6. * @return
  7. * @throws IOException
  8. */
  9. public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
  10. //获取当前类的注解信息
  11. AnnotatedTypeMetadata annotatedTypeMetadata=metadataReader.getAnnotationMetadata();
  12. //获取正在扫描类的信息
  13. ClassMetadata classMetadata=metadataReader.getClassMetadata();
  14. //获取当前类资源(类的路径)
  15. Resource resource = metadataReader.getResource();
  16.  
  17. String className=classMetadata.getClassName();
  18. System.out.println("正在扫描的类:"+className);
  19.  
  20. if(className.contains("Controller")) //类名包含了Controller的才被放到容器中
  21. {
  22. return true;
  23. }
  24.  
  25. return false;
  26.  
  27. }
  28. }
  1.  
  1.  

@ComponentScan 注解的更多相关文章

  1. 005 Spring和SpringBoot中的@Component 和@ComponentScan注解

    今天在看@ComponentScan,感觉不是太理解,下面做一个说明. 1.说明 ComponentScan做的事情就是告诉Spring从哪里找到bean 2.细节说明 如果你的其他包都在使用了@Sp ...

  2. 深入理解spring注解之@ComponentScan注解

    今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1,@ComponentScan注解是什么 其实 ...

  3. springboot @ComponentScan注解

    @ComponentScan 告诉Spring从哪里找到bean. 如果你的其他包都在@SpringBootApplication注解的启动类所在的包及其下级包,则你什么都不用做,SpringBoot ...

  4. 【Spring注解驱动开发】自定义TypeFilter指定@ComponentScan注解的过滤规则

    写在前面 Spring的强大之处不仅仅是提供了IOC容器,能够通过过滤规则指定排除和只包含哪些组件,它还能够通过自定义TypeFilter来指定过滤规则.如果Spring内置的过滤规则不能够满足我们的 ...

  5. Spring学习总结(6)-@Component和@ComponentScan注解

    参考文档:https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-ste ...

  6. Spring源码分析-从@ComponentScan注解配置包扫描路径到IoC容器中的BeanDefinition,经历了什么(一)?

    阅前提醒 全文较长,建议沉下心来慢慢阅读,最好是打开Idea,点开Spring源码,跟着下文一步一步阅读,更加便于理解.由于笔者水平优先,编写时间仓促,文中难免会出现一些错误或者不准确的地方,恳请各位 ...

  7. 构建后端第2篇之---springb @ComponentScan注解使用

    张艳涛写于2021-2-8日 构建后端项目的时候遇到一个问题,在zyt-auth项目的依赖定义了@Component类,这个类在项目启动的时候提示没有找到bean Field tokenService ...

  8. ComponentScan注解的使用

    在项目初始化时,会将加@component,@service...相关注解的类添加到spring容器中. 但是项目需要,项目初始化时自动过滤某包下面的类,不将其添加到容器中. 有两种实现方案, 1.如 ...

  9. Spring学习七:ComponentScan注解

    今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1.ComponentScan注解是什么 其实很 ...

随机推荐

  1. GIT 工作流程常用用命令大全

    一.Git基本工作流程 1.Git工作区域   2.向仓库中添加文件流程 二.Git初始化及仓库创建和操作 1.Git安装之后需要进行一些基本信息设置 a.设置用户名:git  config -- g ...

  2. 创建基本的webpack4.x项目

    1.步骤 1)运行npm init -y 快速初始化项目 2)在项目根目录创建src源代码目录和dist产品目录,目录结构 webpack4.x-base |dist |src |index.html ...

  3. 前端Web浏览器基于Flash如何实时播放监控视频画面(二)之Windows搭建(RTMP)流媒体服务器

    本片文章只是起到抛砖引玉的作用,能从头到尾走通就行,并不做深入研究.为了让文章通俗易懂,尽量使用白话描述. 0x001: 获取 流媒体服务器有很多,这里以nginx为例. nginx for Wind ...

  4. List集合、泛型、装箱拆箱

    1.List集合 Vector:增删改查都慢 线程同步 线程安全 LlinkedList:以链表结构存储数据,查询慢.增删快 ArrayList:的运行速度比较快 连续数据空间存储数据,查询快(下标) ...

  5. MessagePack Java Jackson 在不关闭输出流(output stream)的情况下序列化多变量

    com.fasterxml.jackson.databind.ObjectMapper 在默认的情况下在写出输入后将会关闭输出流(output stream). 如果你希望序列化多值变量在同一个输出流 ...

  6. Navicat Premium12远程连接MySQL数据库

    https://blog.csdn.net/dengjin20104042056/article/details/95091506 方法二: step1: 修改表user mysql> use ...

  7. 2016 ICPC 大连网络赛 部分题解

    先讲1007,有m个人,n种石头,将n种石头分给m个人,每两个人之间要么是朋友关系,要么是敌人关系,朋友的话他们必须有一种相同颜色的石头,敌人的话他们必须所有石头的颜色都不相同.另外,一个人可以不拥有 ...

  8. MS14-068提权

    • Ms14- • 库 • https://github.com/bidord/pykek • ms14-.py -u user@lab.com -s userSID -d dc.lab.com • ...

  9. [CSP-S模拟测试]:Weed(线段树)

    题目描述 $duyege$的电脑上面已经长草了,经过辨认上面有金坷垃的痕迹.为了查出真相,$duyege$准备修好电脑之后再进行一次金坷垃的模拟实验.电脑上面有若干层金坷垃,每次只能在上面撒上一层高度 ...

  10. django CBV模式源码执行过程

    在说CBV模式之前,先看下FBV的url配置方式: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^xxx/', login), ur ...