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. c# MVC Action 如何知道 发送方给你的 Json 数据的格式内容是什么

    public class DemoModel { public string Name { get; set; } public int Age { get; set; } } [HttpPost] ...

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

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

  3. API网关【gateway 】- 2

    最近在公司进行API网关重写,公司内采用serverMesh进行服务注册,调用,这里结合之前学习对API网关服务进行简单的总结与分析. 由于采用了大量的nginx相关的东西,所以在此记录一下: 配置连 ...

  4. CodeForces - 940C + CodeForces - 932B (两道比较好的模拟题)

    940C链接:http://codeforces.com/problemset/problem/940/C C. Phone Numbers time limit per test 2 seconds ...

  5. 伪装为 吃鸡账号获取器 的QQ木马分析

    本文作者:i春秋作家坏猫叔叔 0×01 起因随着吃鸡热潮的来临,各种各样的吃鸡辅助和账号交易也在互联网的灰色地带迅速繁殖滋生.其中有真有假,也不乏心怀鬼胎的“放马人”.吃过晚饭后在一个论坛看到了这样一 ...

  6. window.location API

    概述 今天被自己鄙视了,竟然不会用window.location.search进行页面传值.现在好好总结下window.location API,记录一下供以后开发时参考,相信对其它人也有用. 页面传 ...

  7. SQL高效查询两个表不同的数据

    逻辑相对复杂,但是速度最快: )

  8. Python小白学习之路(二十五)—【装饰器的应用】

    通过一个任务来加深对装饰器的理解和应用 回顾:装饰器的框架 def timmer(func): def wrapper(): func() return wrapper 任务:给以下正在运行的程序加一 ...

  9. POJ 2562

    #include<iostream> #include<algorithm> #define MAXN 15 using namespace std; //int rec[MA ...

  10. 十分钟内在Ubuntu系统上搭建Mono开发环境(Mono软件Ubuntu系统国内镜像源、Mono国内镜像源)

    Mono软件Ubuntu系统国内镜像源.Mono国内镜像源 http://download.githall.cn/repo 替换为国内源(非官方)有利于加快mono的安装速度,一般情况下,完成mono ...