利用spring 自定义注解扫描 找出使用自定义注解的类
2018-03-08 00:00
253 查看
我们常常有扫描项目里带有指定注解的class, 下面是利用spring扫描自定义注解的方法, 还是比较灵活的
我这里将扫描到的class放到map, 你可以放到其他地方,以便后期使用
我这里将扫描到的class放到map, 你可以放到其他地方,以便后期使用
import lombok.extern.slf4j.Slf4j; import org.apache.shiro.SecurityUtils; import org.apache.shiro.session.Session; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; 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.util.ClassUtils; import org.springframework.util.StringUtils; import javax.persistence.Table; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * 扫描指定包下带有指定注解的class * @author jaffreyen * @date 2018/3/5 */ @Slf4j public class MyApplicationListener implements ApplicationListener { /** * Handle an application event. * * @param event the event to respond to */ @Override public void onApplicationEvent(ApplicationEvent event) { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); final String RESOURCE_PATTERN = "/**/*.class"; // 扫描的包名 final String BASE_PACKAGE = "com.xxx"; Map<String,Class<?>> classCache = new HashMap<>(); try { String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(BASE_PACKAGE) + RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader reader = readerFactory.getMetadataReader(resource); //扫描到的class String className = reader.getClassMetadata().getClassName(); Class<?> clazz = Class.forName(className); //判断是否有指定注解 Table annotation = clazz.getAnnotation(Table.class); if(annotation != null){ //这个类使用了自定义注解 classCache.put(className, clazz); } } } } catch (IOException | ClassNotFoundException e) { log.error("读取class失败", e); } } }
相关文章推荐
- JAVAWEB开发之Servlet3.0新特性的使用以及注解的详细使用和自定义注解的方法、动态代理的使用、利用动态代理实现细粒度的权限控制以及类加载和泛型反射
- 自定义注解(2)—定义和使用(利用反射获取)
- 利用自定义注解来配合使用SpirngMVC拦截器
- 利用Sonar定制自定义JS扫描规则(三)——SSLR JavaScript Toolkit 使用说明
- Java注解教程:自定义注解示例,利用反射进行解析
- 关于使用MyBatis-Generator时自定义注解生成器的一些问题
- java扫描包下的类,一般会配合注解使用
- 使用Spring Annotation注解导致jar包更新的文件扫描不到
- excel 导入导出使用poi自定义注解
- Java注解详解,自定义注解,利用反射解析注解
- SpringMVC利用AOP实现自定义注解记录日志
- 利用反射打造自定义注解,自动校验或处理数据
- 解析动态代理模式与跟自定义注解配合使用以及事务注解原理
- 利用Spring AOP自定义注解解决日志和签名校验
- java自定义注解简单使用
- spring源码剖析(五)利用AOP实现自定义Spring注解
- excel导入导出使用poi自定义注解
- Annotation注解的介绍和使用 自定义注解
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- java反射以及自定义注解二者的使用