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. Oracle.ManagedDataAccess.Client.OracleException:“ORA-00936: 缺失表达式”

    static void Main(string[] args) { string sql = "insert into StudentC(Stuid, Stuname, Stupass) v ...

  2. Java正则表达式初学者使用法简介

    在Java中使用正则表达式的方法非常多,最简单的就是和字符串一起使用.对于Java正则表达式初学者,在String中有四个方法可以使用正则表达式,本文正是介绍这四个方法来使用正则表达式来处理文本数据. ...

  3. Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)

    1.首先更新pip版本的时候出现.这是出现在python2.7.16出现的问题 2.进入你的pyhton目录下的Lib\mimetypes.py 打开它 3.在import下面加入这代码 if sys ...

  4. tomcat 启动 证书异常java.io.IOException: Alias name [cas] does not identify a key entry

    在搭建CAS server的过程中,Tomcat开启https,配置秘钥证书,证书是通过keytool生成的 <Connector port=" protocol="org. ...

  5. c++11中关于std::thread的join的思考

    c++中关于std::thread的join的思考 std::thread是c++11新引入的线程标准库,通过其可以方便的编写与平台无关的多线程程序,虽然对比针对平台来定制化多线程库会使性能达到最大, ...

  6. POJ 2719

    #include<iostream> #include<stdio.h> using namespace std; ]; int _pow(int m,int n); int ...

  7. FireFox浏览器-xpath快速定位插件:Xpath Checker

    FireFox浏览器-xpath快速定位插件:Xpath Checker 插件截图:

  8. 阿里语音识别(语音转文字)java调用全程手把手详解-适合中小学生快速上手

    阿里语音识别服务java调用全程手把手详解-适合中小学生快速上手 阿里语音识别与百度语音识别的调用对比: 用例:1分30秒的录音文件    百度用时:3秒    阿里用时:30秒    识别准确率来看 ...

  9. 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 3、Python Basics with numpy (optional)

    Python Basics with numpy (optional)Welcome to your first (Optional) programming exercise of the deep ...

  10. Java操作word文档使用JACOB和POI操作word,Excel,PPT需要的jar包

    可参考文档: http://wibiline.iteye.com/blog/1725492 下载jar包 http://download.csdn.net/download/javashixiaofe ...