spring扫描annotation
来源:互联网 发布:mac菜单栏显示器图标 编辑:程序博客网 时间:2024/05/21 07:49
import java.io.IOException;import java.lang.annotation.Annotation;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Set;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import org.springframework.core.type.classreading.CachingMetadataReaderFactory;import org.springframework.core.type.classreading.MetadataReader;import org.springframework.core.type.classreading.MetadataReaderFactory;import org.springframework.core.type.filter.AnnotationTypeFilter;import org.springframework.core.type.filter.TypeFilter;import org.springframework.util.ClassUtils;/** * 扫描包中的类 */public class LoadPackageClasses { private static final String RESOURCE_PATTERN = "/**/*.class"; private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); private List<String> packagesList = new LinkedList<String>(); private List<TypeFilter> typeFilters = new LinkedList<TypeFilter>(); private Set<Class<?>> classSet = new HashSet<Class<?>>(); /** *eg: <pre name="code" class="java"> * String[] cs = new String[1]; * cs[0] = "com.wz.test"; * LoadPackageClasses l = new LoadPackageClasses(cs, * org.springframework.stereotype.Controller.class); * l.getClassSet(); */ public LoadPackageClasses ( String[] packagesToScan, Class<? extends Annotation>... annotationFilter) { if (packagesToScan != null) { for (String packagePath : packagesToScan) { this.packagesList.add(packagePath); } } if (annotationFilter != null) { for (Class<? extends Annotation> annotation : annotationFilter) { typeFilters.add(new AnnotationTypeFilter(annotation, false)); } } } public Set<Class<?>> getClassSet() throws IOException, ClassNotFoundException { this.classSet.clear(); if (!this.packagesList.isEmpty()) { for (String pkg : this.packagesList) { String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN; Resource[] resources = this.resourcePatternResolver.getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader reader = readerFactory.getMetadataReader(resource); String className = reader.getClassMetadata().getClassName(); if (matchesEntityTypeFilter(reader,readerFactory)) { this.classSet.add(Class.forName(className)); } } } } } return this.classSet; } /** * 检查注解 */ private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { if (!this.typeFilters.isEmpty()) { for (TypeFilter filter : this.typeFilters) { if (filter.match(reader, readerFactory)) { return true; } } } return false; }}
0 0
- spring扫描annotation
- Spring自定义Annotation扫描
- 关于spring annotation扫描目录的定义
- 正确实现用spring扫描自定义的annotation
- 正确实现用spring扫描自定义的annotation
- 正确实现用spring扫描自定义的annotation
- Spring Annotation
- Spring Annotation
- spring annotation
- Spring annotation
- spring annotation
- spring annotation
- spring annotation
- Spring Annotation
- Spring Annotation
- spring-annotation
- spring-Annotation
- spring annotation
- XML与html的区别
- 入门指南
- android-- 简单自定义安全键盘(只能输入字母,数字,部分符号)
- TextView属性大全+单行显示长文本
- 用CNN工具箱对自己的数据集分类
- spring扫描annotation
- struts2框架使用--ajax重复提交
- 自定义ContentProvider及使用ContentResolver解析详细步骤
- MyEclipse的 at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>错误解决办
- Python 进阶 —— 重访 tuple
- http下载 文件 txt或者jpg图片
- Android Material Design 兼容库的使用详解
- Java中的基础----多线程的实现
- 前端