@ComponentScan(value = "com.annotation",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,ControllerAdvice.class})})
//源码
package org.springframework.context.annotation;
public enum FilterType {
ANNOTATION,//注解类型
ASSIGNABLE_TYPE,//指定类型
ASPECTJ,//按照Aspectj表达式基本用不到
REGEX,//正则表达式
CUSTOM;//自定义
private FilterType() {
}
}
CUSTOMER类型的用法:
@ComponentScan(value = "com.annotation",useDefaultFilters = false,
includeFilters = {
// @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,ControllerAdvice.class}),
// @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {PersonDao.class}),
@ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyFilterType.class})
}
)
其中MyFilterType是自己写的一个类,它需要实现TypeFilter接口,实现接口需要重写一个方法match(),当match()方法返回true,则当前扫描的类被放入spring容器中,返回false则不放入容器中。我这里规定类名中包含“Employee”的返回true,即将其放入到容器中,否则返回false不放入到容器中:
public class MyFilterType implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
if (metadataReader.getClassMetadata().getClassName().contains("Department")){
//获取当前类注解的信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
for (String s : annotationMetadata.getAnnotationTypes()) {
System.out.println("当前正在被扫描的类注解类型" + s);
}
//获取当前正在扫描类的信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
System.out.println("当前正在被扫描的类的类名" + classMetadata.getClassName());
//获取当前类的资源信息(类存放的路径...)
Resource resource = metadataReader.getResource();
System.out.println("当前正在被扫描的类存放的地址" + resource.getURL());
return true;
}
return false;
}
}
当前正在被扫描的类的类名com.annotation.entities.Department
当前正在被扫描的类存放的地址file:/D:/DevInstall/Project/annotation/target/classes/com/annotation/entities/Department.class
当前正在被扫描的类注解类型org.springframework.web.bind.annotation.ControllerAdvice
当前正在被扫描的类的类名com.annotation.handler.DepartmentHandler
当前正在被扫描的类存放的地址file:/D:/DevInstall/Project/annotation/target/classes/com/annotation/handler/DepartmentHandler.class
当前正在被扫描的类注解类型org.springframework.stereotype.Service
当前正在被扫描的类的类名com.annotation.service.DepartmentService
当前正在被扫描的类存放的地址file:/D:/DevInstall/Project/annotation/target/classes/com/annotation/service/DepartmentService.class