组件注册 02 @ComponentScan 自动扫描组件 & 指定扫描规则

资源准备

package com.example.annotations.controller;import org.springframework.stereotype.Controller;@Controller
public class BookController {}
package com.example.annotations.service;import org.springframework.stereotype.Service;@Service
public class BookService {}

传统方式

beans.xml中:

<context:component-scan base-package="com.example.annotations.controller"></context:component-scan><bean id="person" class="com.example.annotations.bean.Person"><property name="name" value="lxl"/><property name="age" value="21"/></bean>

此后 只要在该包下标注了 @Controller @Service @Repository @Component 都会被加载进IOC容器
测试:

    @Testvoid packageScanTest(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();for (String str:beanDefinitionNames){System.out.println(str);}}

有person是因为在beans.xml手动注入的

注解方式 @ComponentScan()

使用的时候注释掉 beans.xml中的 包扫描属性

<!-- 这行 注释掉 -->
<!--    <context:component-scan base-package="com.example.annotations.controller"></context:component-scan>-->    <bean id="person" class="com.example.annotations.bean.Person"><property name="name" value="lxl"/><property name="age" value="21"/></bean>

直接扫描

  1. 在MainConfig上加上注解 @Configuration(value = “com.example.annotations.service”)

    // @Configuration : 告诉Spring这是个配置类
    @Configuration
    @ComponentScan(value = "com.example.annotations")  // 包扫描注解:只要在该包下标注了 @Controller @Service @Repository @Component 都会被加载进IOC容器
    public class MainConfig {//给容器注入一个bean,类型为返回值的类型,id默认使用方法的id@Bean("person")public Person getPerson(){return new Person("qd",21);}
    }
    
  2. 测试

        @Testvoid packageScanTest(){//xml
    //        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");//配置类测试AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();for (String str:beanDefinitionNames){System.out.println(str);}}
    
  3. 结果:
    注入进容器的id默认是类名的小写(不严格是小写,实际为驼峰命名)
    不玩钱

    为什么会有mainConfig?MainConfig.class上标注了@Configuration注解

    @Configuration 源码

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component   // 有这个注解,会被加载进容器
    public @interface Configuration {@AliasFor(annotation = Component.class)String value() default "";boolean proxyBeanMethods() default true;
    }
    

过滤规则

@includeFilters

// @Configuration : 告诉Spring这是个配置类
@Configuration
@ComponentScan(value = "com.example.annotations",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = { Service.class,Controller.class})},useDefaultFilters = false
)
public class MainConfig {//给容器注入一个bean,类型为返回值的类型,id默认使用方法的id@Bean("person")public Person getPerson(){return new Person("qd",21);}
}

测试:

@excludeFilters

???

// @Configuration : 告诉Spring这是个配置类
@Configuration
@ComponentScan(value = "com.example.annotations",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class, Service.class})},useDefaultFilters = false
)
public class MainConfig {//给容器注入一个bean,类型为返回值的类型,id默认使用方法的id@Bean("person")public Person getPerson(){return new Person("qd",21);}
}

测试:可看到过滤掉了controller和Service

【SpringBoot_ANNOTATIONS】组件注册 02 @ComponentScan 自动扫描组件 指定扫描规则相关推荐

  1. 组件注册@ComponentScan的自动扫描和指定扫描规则

    扫描组件@ComponentScan可以自动扫描包以内的含有@Service,@Controller,@Repository,@Component的组件到IOC容器里面去 @ComponentScan ...

  2. Spring Boot基础学习笔记12:组件注册整合Servlet三大组件

    文章目录 零.学习目标 一.整合Servlet三大组件概述 (一)组件注册整合Servlet三大组件 (二)路径扫描整合Servlet三大组件 二.创建Spring Boot项目 - Integrat ...

  3. 组件注册——@ComponentScan自动扫描组件指定扫描规则

    包扫描.只要标注了@Controller.@Service.@Repository,@Component IOCTest.java package com.atguigu.test;import or ...

  4. 02-组件注册-@ComponentScan-自动扫描组件指定扫描规则

    目录 1.配置文件方式:加入context命名空间导入到配置文件 2.主配置类方式:配置类中添加自动扫描组件注解 @ComponentScan 实际在开发中包扫描用的比较多,注解为@Component ...

  5. Spring注解——使用@ComponentScan自动扫描组件

    1.创建一个配置类,在配置类上添加 @ComponentScan 注解.该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <context:component-scan>. pac ...

  6. Spring注解详解:@ComponentScan自动扫描组件使用

    目录 无注解方式component-scan使用 注解方式@ComponentScan使用 @ComponentScan的扫描规则 无注解方式component-scan使用 之前,我们需要扫描工程下 ...

  7. vue封装自己的组件库 02.封装dialog组件

    link: https://blog.csdn.net/weixiaowei_2016/article/details/104702793 四.封装一个element-ui风格的dialog组件 前置 ...

  8. js模板字符串自定义类名_【Vue.js 入门到实战教程】07Vue 组件注册 | 基本使用和组件嵌套...

    来源 | https://xueyuanjun.com/post/21929除了前面介绍的基本语法之外,Vue.js 还支持通过组件构建复杂的功能模块,组件可以称得上是 Vue.js 的灵魂,是 Vu ...

  9. Spring注解开发系列Ⅰ--- 组件注册(上)

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文 ...

  10. Vue第二天学习总结—— Vue全家桶之组件化开发(组件化开发思想、组件注册、Vue调试工具用法、组件间数据交互传递、组件插槽、基于组件的案例——购物车)

    (一) 组件化开发思想 1. 现实中的组件化思想体现 组件化即是对某些可以进行复用的功能进行封装的标准化工作 标准:要想组件能够成功组合在一起,每个组件必须要有标准 分治:将不同的功能封装到不同的组件 ...

最新文章

  1. 常惠琢 201771010102《面向对象程序设计(java)》第七周学习总结
  2. Linux常用的基本命令02
  3. mitmproxy配置代理后 APP连接不到网络 XPosed安装
  4. ​​毕业论文选题三步法
  5. 何樱c语言,C语言程序设计-电子教案-连卫民(442页)-原创力文档
  6. 前端开发怎么用php,做web前端开发怎么样?
  7. js获取session_学习后端鉴权系列: 基于Cookie, Session认证
  8. 统计学习方法c++实现之二 k近邻法
  9. UDS协议入门之10服务
  10. 幼儿园编程机器人课程介绍
  11. Swift 圆形进度条
  12. python教材答案第四章_第四章-习题答案
  13. CATIA—机电产品数字化设计工具
  14. (泰勒展开式/欧拉公式)证明:e^x推导及e^(iπ) = -1展开过程
  15. cd linux menu.lst,Windows 7 中使用 grldr + menu.lst 引導 linux系統和win7.
  16. 十个关于商业智能商业智能BI的观点,你认同几个?
  17. 数据结构与算法-基础(十二)B 树
  18. 计算机毕业设计Python+uniapp养花助手小程序(小程序+源码+LW)
  19. Redis的初步使用教程
  20. pythonweb数据可视化_独家 | 基于Python实现交互式数据可视化的工具(用于Web)

热门文章

  1. 2018年中国财富管理才真正迎来发展元年
  2. 如何对国内股票的名称进行脱敏(待续)
  3. CTP: 初始化成功和TradeAPI、MDAPI登陆成功
  4. 技术人 | 我在支付宝体验技术部这四年学到了什么?
  5. 【交通标志识别】基于matlab GUI模板匹配交通标志识别【含Matlab源码 1059期】
  6. 【图像修复】基于matlab GUI维纳滤波图像复原【含Matlab源码 851期】
  7. 【语音隐写】基于matlab小波变换算法求解水印嵌入提取【含Matlab源码 513期】
  8. adb脚本选择语句_Python开发:王者荣耀自动刷金币脚本
  9. pytorch使用模型预测_使用PyTorch从零开始对边界框进行预测
  10. torch.optim.lr_scheduler.MultiStepLR()用法研究 台阶/阶梯学习率