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. 「PKUSC2018」真实排名(排列组合,数学)

    前言 为什么随机跳题会跳到这种题目啊? Solution 我们发现可以把这个东西分情况讨论: 1.这个点没有加倍 这一段相同的可以看成一个点,然后后面的都可以. 这一段看成一个点,然后前面的不能对他造 ...

  2. Akka(42): Http:身份验证 - authentication, authorization and use of raw headers

    当我们把Akka-http作为数据库数据交换工具时,数据是以Source[ROW,_]形式存放在Entity里的.很多时候除数据之外我们可能需要进行一些附加的信息传递如对数据的具体处理方式等.我们可以 ...

  3. 【入门推荐】SQL注入进行WebShell渗透测试的基础概览

    作者:zero 本文为SQL基本注入的进阶文章,如有任何疑问请查看: SQL基本注入演示:https://www.cnblogs.com/anbus/p/10082452.html 导语: 利用SQL ...

  4. SpringMVC初探-HelloWorld

    MVC的概念 MVC是一种设计模式,即Model--View-Controller,模型--视图--控制器 Model(模型)表示应用程序核心(比如数据库记录列表). View(视图)显示数据(数据库 ...

  5. 【6】JMicro微服务-服务日志监控

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl   1. 微服务相关 在前面的1到5节中,总共涉及服务提供者,服务消费者,服务监听服务,发布订阅服务,熔断器服务5种类型的猪 ...

  6. 关于a标签的onclick与href的执行顺序

    onclick的事件被先执行,其次是href中定义的(页面跳转或者javascript), 同时存在两个定义的时候(onclick与href都定义了),如果想阻止href的动作,在onclick必须加 ...

  7. POJ 2864

    #include <iostream> #define MAXN 600 using namespace std; int _m[MAXN][MAXN]; int main() { //f ...

  8. 平衡二叉树-AVL树(LL、RR、LR、RL旋转)

    平衡二叉树的定义: 任意的左右子树高度差的绝对值不超过1,将这样的二叉树称为平衡二叉树,二叉平衡树前提是一个二叉排序树. 平衡二叉树的插入: 二叉平衡树在插入或删除一个结点时,先检查该操作是否导致了树 ...

  9. JavaScript -- Math

    ----- 016-Math.html ----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...

  10. C#基础篇十小练习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace P03练 ...