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是线程安全 ...
随机推荐
- 宽度优先搜索--------迷宫的最短路径问题(dfs)
宽度优先搜索运用了队列(queue)在unility头文件中 源代码 #include<iostream>#include<cstdio>#include<queue&g ...
- CSS3多栏布局
CSS3多栏布局 分栏数: column-count:auto|num: auto为默认值,表示元素只有一列.num取值为大于0的整数 每栏宽度: column-width:auto|<leng ...
- BUUCTF-web ZJCTF,不过如此
很明显要利用伪协议读next.php base64解码后查看源码 <?php $id = $_GET['id']; $_SESSION['id'] = $id; function complex ...
- three.js 数学方法之Matrix4
今天郭先生说一说three.js中的Matrix4,相较于Matrix3来说,Matrix4和three.js联系的更紧密,因为在4x4矩阵最常用的用法是作为一个变换矩阵.这使得表示三维空间中的一个点 ...
- c++输出左右对齐设置
#include<iostream> int main(){ using std::cout; cout.setf(std::ios::left); int w = cout.width( ...
- excel文件双击打开空白
excel文件双击打开之后进入软件,没有去读文件 一.现象描述 打开现象如下所示,只有excel模板,看不到excel中的表格模板. 二.想要打开文件 (1)在软件的文件--->打开--> ...
- 基于个人理解的springAOP部分源码分析,内含较多源码,慎入
本文源码较多,讲述一些个人对spring中AOP编程的一个源码分析理解,只代表个人理解,希望能和大家进行交流,有什么错误也渴求指点!!!接下来进入正题 AOP的实现,我认为简单的说就是利用代理模式,对 ...
- PHP shuffle() 函数
实例 把数组中的元素按随机顺序重新排列: <?php$my_array = array("red","green","blue",&q ...
- PHP pos() 函数
实例 输出数组中的当前元素的值: <?php$people = array("Peter", "Joe", "Glenn", &quo ...
- PHP get_html_translation_table() 函数
实例 输出 htmlspecialchars 函数使用的翻译表: <?php高佣联盟 www.cgewang.comprint_r (get_html_translation_table()); ...