ComponentScan,只写入value,可扫描路径下装配的@Contrller、@Service、@Repository

 @ComponentScan(value = "com.test")

测试类:

 @Test
@SuppressWarnings("resource")
public void test01() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
MainConfig.class); String[] definitionNames = applicationContext.getBeanDefinitionNames();//获取spring装配的bean for (String name : definitionNames) {
System.out.println(name);
}
}

ComponentScan中还有参数excludeFilters、includeFilters、useDefaultFilters

ANNOTATION, // 按照注解 比如Controller Service
ASSIGNABLE_TYPE,// 按照给定的类型 比如某个类 like this

CUSTOM,//自定义验证

其中excludeFilters是设置屏蔽,excludeFilters可通过设置type进行过滤,classes进行详细的排除。其中使用方法如下:

 @ComponentScan(value = "com.test", excludeFilters =
{ @Filter(type = FilterType.ASSIGNABLE_TYPE ,
classes={BookService.class,Book.class}) })

通过注解类型屏蔽BookService及Book类

1 @ComponentScan(value = "com.test", excludeFilters =
2 { @Filter(type = FilterType.ASSIGNABLE_TYPE ,
3 classes={BookService.class,Book.class}) })

通过注解类型屏蔽service,controller

 @ComponentScan(value = "com.test", excludeFilters =
{ @Filter(type = FilterType.ANNOTATION ,
classes={Controller.class,Service.class}) })

而includeFilters是设置非屏蔽,只装配自己设定的类,要注意的是,在使用是要将useDefaultFilters关闭,默认时开启的。

只装配配置了service,controller的类

 @ComponentScan(value = "com.test", useDefaultFilters=false,includeFilters =
{ @Filter(type = FilterType.ANNOTATION ,
classes={Controller.class,Service.class}) })

ComponentScans的使用就更明了,里面放的是多个ComponentScan,直接放进去就可以直接使用

 @ComponentScans(
value={
@ComponentScan(value = "com.test", useDefaultFilters=false,includeFilters =
{ @Filter(type = FilterType.ANNOTATION ,
classes={Controller.class}) })
}
)

CUSTOM自定义验证,需要我们写一个类,去实现TypeFilter的方法,下面测试类,对className中包含了“ser”的允许装配进容器

 public class MyTypeFilter implements TypeFilter {

     public boolean match(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) throws IOException {
// TODO Auto-generated method stub
// 获取当前类注解的信息
AnnotationMetadata annotationMetadata=metadataReader.getAnnotationMetadata();
//获取当前正在扫描的类信息
ClassMetadata classMetadata=metadataReader.getClassMetadata();
//获取当前类资源(类的路径)
Resource resource=metadataReader.getResource(); String className=classMetadata.getClassName(); System.out.println("--->"+className);
if(className.contains("ser")){
return true;
} return false;
} }
 @ComponentScans(value={@ComponentScan
(value = "com.test", useDefaultFilters=false,
includeFilters = {@Filter(type=FilterType.CUSTOM ,classes={MyTypeFilter.class})}
)}
)

spring装配注解(IOC容器加载控制)ComponentScan及ComponentScans使用的更多相关文章

  1. Spring之IOC容器加载初始化的方式

    引言 我们知道IOC容器时Spring的核心,可是如果我们要依赖IOC容器对我们的Bean进行管理,那么我们就需要告诉IOC容易他需要管理哪些Bean而且这些Bean有什么要求,这些工作就是通过通过配 ...

  2. Spring源码:Spring IoC容器加载过程(1)

    Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...

  3. Spring源码:Spring IoC容器加载过程(2)

    Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...

  4. spring boot注解 --@spring-boot-devtools 自动加载修改的文件和类

    spriing boot中有一个注解,是自动加载修改后的类或者文件. 使用方法为: spring-boot-devtools=true 需要引入devtools包依赖: <dependency& ...

  5. 梳理源码:spring ioc容器加载的流程图

  6. spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)转

    关键字:spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) 应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用 ...

  7. spring 容器加载完成后执行某个方法

    理论 刚好再开发过程中遇到了要在项目启动后自动开启某个服务,由于使用了spring,我在使用了spring的listener,它有onApplicationEvent()方法,在Spring容器将所有 ...

  8. spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)

    关键字:spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) 应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用 ...

  9. spring boot容器加载完后执行特定操作

    有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情.这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情 1.Applicat ...

随机推荐

  1. 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView

    [源码下载] 背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView 作者:webabcd 介绍背水一战 Windows 1 ...

  2. Spring IOC 容器源码分析 - 创建原始 bean 对象

    1. 简介 本篇文章是上一篇文章(创建单例 bean 的过程)的延续.在上一篇文章中,我们从战略层面上领略了doCreateBean方法的全过程.本篇文章,我们就从战术的层面上,详细分析doCreat ...

  3. 常用下载方式的区别-BT下载、磁力链接、电驴

    出处:https://www.jianshu.com/p/72b7a64e5be1 打开 115 离线下载的窗口,看到支持这么多种链接,你都清楚他们是什么原理嘛?接下来我们一个一个说. 一.HTTP( ...

  4. MySQL 中文字符集排序

    SELECT 字段名 FROM 表 ORDER BY CONVERT(字段名 USING gbk) ASC;

  5. mysql 存储过程 游标嵌套

    基本表temp 包括 name, type, sendCity, getCity 分别对应物流送货司机名, 倒车的第几段, 发货城市, 收货城市 表结构 -- -------------------- ...

  6. Microsoft SQL Server on Linux 踩坑指南

    微软用 SQL Server 在 2016 年的时候搞了一个大新闻,宣传 Microsoft ❤️ Linux 打得一众软粉措手不及.但是这还是好事情,Linux 上也有好用的 SQL Server ...

  7. .NET跨平台实践:再谈用C#开发Linux守护进程 — 完整篇

    Linux守护进程是Linux的后台服务进程,相当于Windows服务,对于为Linux开发服务程序的朋友来说,Linux守护进程相关技术是必不可少的,因为这个技术不仅仅是为了开发守护进程,还可以拓展 ...

  8. 剑指offer十七姊妹篇之二叉树的创建、遍历、判断子二叉树

    1.二叉树节点类 public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public Tr ...

  9. (转)Python开发程序:支持多用户在线的FTP程序

    原文链接:http://www.itnose.net/detail/6642756.html 作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ...

  10. linux使用find和crontab命令定期清理过期文件

    crontab 命令 crontab 命令是 Linux 中用来设定重复执行命令或脚本的工具.它能够在指定的时间段内,按照需求以某一时间间隔执行命令或脚本. crontab 的基本用法 crontab ...