Spring注解驱动开发01(组件扫描使用详解)
使用Spring注解代替XML的方式
以前都是通过xml配bean的方式来完成bean对象放入ioc容器,即使通过@Aotuwire自动装配bean,还是要创建一个xml文件,进行包扫描,显得过于繁琐
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 包扫描 属性:use-default-filters="false" 禁用全扫描-->
<context:component-scan base-package="com.atguigu">
<!-- 排除标注了service注解的类扫描-->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>-->
<!-- 只扫描service注解的类,默认全扫描,需要先禁用默认规则才能使用-->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>-->
</context:component-scan>
<bean id="r1" class="com.atguigu.aop.service.BookService"/>
</beans>
使用注解的方式将bean组件加入到ioc容器
- 创建一个配置类
//此注解告诉了spring这是一个配置类
@Configuration
//扫描com.xxx下的组件,将其加入到ioc容器
@ComponentScan(value = "com.xxx")
public class MainConfig {
//将@Bean标注的组件加入到ioc容器,默认为方法名作为id,返回值作为bean类型
@Bean
public Person person(){
return new Person();
}
}
- 测试ioc中已有的组件
//AnnotationConfigApplicationContext 加载注解配置,获取ioc容器
try (ConfigurableApplicationContext ioc = new AnnotationConfigApplicationContext(MainConfig.class)) {
Person bean = ioc.getBean(Person.class);
PersonController controller = ioc.getBean(PersonController.class);
PersonServic servic = ioc.getBean(PersonServic.class);
PersonDao dao = ioc.getBean(PersonDao.class);
System.out.println(bean);
System.out.println(controller);
System.out.println(dao);
System.out.println(servic);
}
打印结果
com.atguigu.pojo.Person@1ddf84b8
com.atguigu.controller.PersonController@1139b2f3
com.atguigu.dao.PersonDao@7a69b07
com.atguigu.service.PersonServic@5e82df6a
注解@ComponentScan
的使用
修饰位置
@Target({ElementType.TYPE}) 只能修饰在类上
- value属性
//扫描com.xxx下的组件,将其加入到ioc容器
@ComponentScan(value = "com.xxx")
- 排除和包含
可通过相关属性来排除不需要加入到ioc容器的组件,或者指定ioc中只包含该组件
- 排除excludeFilters
//看源码,类型为ComponentScan.Filter[]
ComponentScan.Filter[] excludeFilters() default {};
//所以传入该类型参数
@ComponentScan(value = "com.atguigu",excludeFilters = {
//不扫描@controller和service注解,type默认为注解类型,可以不写
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})})
@ComponentScan.Filter中的type的形式
- 通过注解
type = FilterType.ANNOTATION,
classes = {Controller.class, Service.class}
表示通过注解类型来筛选需要排除的类,classes是一个数组,可以申明多个
表示被@Cotroller或者被@Service修饰的类都不加入到ioc容器
- 通过类型
type = ASSIGNABLE_TYPE,
classes = {PersonController.class, PersonService.class}
表示通过类的类型来筛选需要排除的类,classes是一个数组,可以申明多个
表示为PersonCotroller类型的组件或者被PersonService类型的组件都不加入到ioc容器
- 正则类型
type = FilterType.REGEX
- 自定义过滤类型,需要写一个TypeFilter的实现类
创建实现类
/**
* @param metadataReader 当前正在扫描的类的信息
* @param metadataReaderFactory 可以获取到其它任何类信息
* @return 是否执行过滤
*/
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
//获取当前类注解的信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
//获取当前正在扫描的类的定义信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//获取当前类的资源信息(类的路径)
Resource resource = metadataReader.getResource();
String className = classMetadata.getClassName();
System.out.println(className);
if (className.contains("Dao")){ //如果类名包含了Dao,则执行过滤
return true;
}
return false;
}
使用
//指定type
type = FilterType.CUSTOM,
classes = {Myfilter.class} //自己写的类信息
打印结果
加上dao
Person bean = ioc.getBean(Person.class);
PersonController controller = ioc.getBean(PersonController.class);
PersonServic servic = ioc.getBean(PersonServic.class);
PersonDao dao = ioc.getBean(PersonDao.class);
System.out.println(bean);
System.out.println(controller);
System.out.println(dao);
System.out.println(servic);
抱错,找不到dao,因为已经被过滤了
No qualifying bean of type 'com.atguigu.dao.PersonDao' available
注释掉dao
// 打印结果没问题
com.atguigu.pojo.Person@7a69b07
com.atguigu.controller.PersonController@5e82df6a
com.atguigu.service.PersonServic@3f197a46
- 包含includeFilters,用法类似,我就不多说了
Spring注解驱动开发01(组件扫描使用详解)的更多相关文章
- 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则
写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...
- Spring注解驱动开发(一)-----组件注册
注册bean xml方式 1.beans.xml-----很简单,里面注册了一个person bean <?xml version="1.0" encoding=" ...
- 0、Spring 注解驱动开发
0.Spring注解驱动开发 0.1 简介 <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作 ...
- 【Spring注解驱动开发】使用@Import注解给容器中快速导入一个组件
写在前面 我们可以将一些bean组件交由Spring管理,并且Spring支持单实例bean和多实例bean.我们自己写的类,可以通过包扫描+标注注解(@Controller.@Servcie.@Re ...
- 【spring 注解驱动开发】spring组件注册
尚学堂spring 注解驱动开发学习笔记之 - 组件注册 组件注册 1.@Configuration&@Bean给容器中注册组件 2.@ComponentScan-自动扫描组件&指定扫 ...
- 【Spring注解驱动开发】使用@Scope注解设置组件的作用域
写在前面 Spring容器中的组件默认是单例的,在Spring启动时就会实例化并初始化这些对象,将其放到Spring容器中,之后,每次获取对象时,直接从Spring容器中获取,而不再创建对象.如果每次 ...
- 【Spring注解驱动开发】自定义TypeFilter指定@ComponentScan注解的过滤规则
写在前面 Spring的强大之处不仅仅是提供了IOC容器,能够通过过滤规则指定排除和只包含哪些组件,它还能够通过自定义TypeFilter来指定过滤规则.如果Spring内置的过滤规则不能够满足我们的 ...
- 【Spring注解驱动开发】如何实现方法、构造器位置的自动装配?我这样回答让面试官很满意!
在 冰河技术 微信公众号前面的文章中,我们介绍了如何使用注解来自动装配Spring组件.之前将的都是在来的字段上添加注解,那有没有什么方法可以实现方法.构造器位置的自动装配吗?今天我们就一起来探讨下如 ...
- 【Spring注解驱动开发】聊聊Spring注解驱动开发那些事儿!
写在前面 今天,面了一个工作5年的小伙伴,面试结果不理想啊!也不是我说,工作5年了,问多线程的知识:就只知道继承Thread类和实现Runnable接口!问Java集合,竟然说HashMap是线程安全 ...
随机推荐
- Jarvisoj-web Login
题目入口: http://web.jarvisoj.com:32772/ 有个登陆框,随便提交参数然后bp抓包 get到了一个Hint,给了sql查询的语句 select * from `admin` ...
- vb教程图文并茂
https://blog.csdn.net/baimafujinji/article/details/70198953
- laravel 缓存相关常用操作
//----------设置缓存----------- //Cache::put($key,$val,$minutes); 如果$key已存在,则覆盖原有值 Cache::put('name', '张 ...
- python学习笔记1 -- 函数式编程之高阶函数 map 和reduce
我用我自己,就是高阶函数,直接表现就是函数可以作为另一个函数的参数,也可以作为返回值 首先一个知识点是 函数的表现形式,印象中的是def fw(参数)这种方式定义一个函数 python有很多的内置函 ...
- 记一次针对静态页面的DDOS基本防护
可以说是我试图进入安全口的天才第一步了,能走多远鬼知道呢 背景 去年年前接到的一个外包项目,是一个base在日本的中国人留学机构做的静态页面.出于锻炼自己的目的,选择为他们按次结薪做长期服务维护.20 ...
- PHP date_sunset() 函数
------------恢复内容开始------------ 实例 返回葡萄牙里斯本今天的日落时间: <?php// Lisbon, Portugal:// Latitude: 38.4 Nor ...
- bzoj 1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 最大流+二分
题目要求所有牛都去避雨的最长时间最小. 显然需要二分 二分之后考虑如何判定. 显然每头牛都可以去某个地方 但是前提是最短路径<=mid. 依靠二分出来的东西建图.可以发现这是一个匹配问题 din ...
- 构建自己的js库
一.背景 web前端开发人员经常会用到一些现成的js库(框架).框架的使用增加了代码的模块化和可复用性,最主要的是屏蔽了浏览器之间差异性的实现,使得代码更加简洁,框架使用者只需要将注意力放在业务的实现 ...
- 关于idea 在创建maven 骨架较慢问题解决
在设置中->maven>runner>VM Options 粘贴 -DarchetypeCatalog=internal 其中 -D archetype:原型,典型的意思 ( ...
- 【PA2014】Bohater 题解(贪心)
前言:一道经典贪心题. -------------------------- 题目链接 题目大意:你有$z$滴血,要打$n$只怪.打第$i$只怪扣$d_i$滴血,回$a_i$滴血.问是否存在一种能够通 ...