Scan classes with specified annotation under specified package.
steps:
* define annotation
* use annotation on class
* get class set with specified annotations under specified packages,
2 approach:
* spring (preferred)
use util class from spring:
ClassPathScanningCandidateComponentProvider
* reflections lib
this lib provide class/methods to do this,
but it's not as good as spring,
*
* iterate the class set, read annotation, take actions as need,
*
maven:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.9.9-RC1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency>
Util class:
package eric.j2se.anno; import java.lang.annotation.Annotation; import java.util.HashSet; import java.util.Set; import org.reflections.Reflections; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.core.type.filter.AnnotationTypeFilter; /** * Util to get classes according to annotation on class in a specified package. * * @author eric * @date Aug 5, 2014 12:36:42 PM */ public class PkgAnnoUtil { /** * * <p> * Scan class with specified annotation under specific packages, using util from spring. * </p> * <p> * Sub package & inner class will be included. * </p> * * @param pkgArray * an array of package path, * @param annoClazzArray * an array of annotation class, * @return */ public static Set<BeanDefinition> getBeanSetWithAnno(String pkgArray[], Class<? extends Annotation>[] annoClazzArray) { // prepare scanner, with each annotation, ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true); for (Class<? extends Annotation> annoclazz : annoClazzArray) { scanner.addIncludeFilter(new AnnotationTypeFilter(annoclazz)); } Set<BeanDefinition> beanSet = null; // search with each package, and combine search result, for (String pkg : pkgArray) { if (beanSet == null) { beanSet = scanner.findCandidateComponents(pkg); } else { beanSet.addAll(scanner.findCandidateComponents(pkg)); } } return beanSet; } /** * <p> * Scan class with specified annotation under a specific package, using a lib called "reflections". * </p> * <p> * Sub package & inner class will be included. * </p> * <p> * This method is deprecated, use getBeanSetWithAnno() instead. * </p> * * @param pkg * package path * @param annoClazz * annotation class * @return a set of class, or null if error occur, */ @Deprecated public static Set<Class<? extends Object>> getClazzSetWithAnno(String pkg, Class<? extends Annotation> annoClazz) { // get class set Set<Class<? extends Object>> clazzSet = new Reflections(pkg).getTypesAnnotatedWith(annoClazz); // get class set that with a specific annotation Set<Class<? extends Object>> clazzWithAnnoSet = new HashSet<Class<? extends Object>>(); for (Class<? extends Object> clazz : clazzSet) { if (clazz.getAnnotation(annoClazz) != null) { clazzWithAnnoSet.add(clazz); } } return clazzWithAnnoSet; } }
junit test:
package eric.j2se.anno; import java.lang.reflect.Method; import java.util.Set; import junit.framework.TestCase; import org.junit.Test; import org.springframework.beans.factory.config.BeanDefinition; public class PkgAnnoUtilTest extends TestCase { private Class<SimpleAnno> annoClazz = SimpleAnno.class; // annotation to filter class, private String pkg = "eric.j2se.anno.annopkg"; // package name to be search, private String methodName = "takeAction"; // name of method to execute, private int annotatedClassCount = 4; // count of class that is annotated, /** * test - getBeanSetWithAnno(), */ @Test @SuppressWarnings("unchecked") public void testGetBeanSetWithAnno() { System.out.println("------ annotation scan - spring - start ------"); // get classes with in package, and has specified annotation, Set<BeanDefinition> beanWithAnnoSet = PkgAnnoUtil.getBeanSetWithAnno(new String[] { pkg }, new Class[] { annoClazz }); assertEquals(beanWithAnnoSet.size(), annotatedClassCount); try { // execute a specific method, for (BeanDefinition bean : beanWithAnnoSet) { Class<? extends Object> clazz = Class.forName(bean.getBeanClassName()); Method md = clazz.getMethod(methodName); if (md != null) { md.invoke(clazz.newInstance()); } } } catch (Exception e) { e.printStackTrace(); } System.out.println("------ annotation scan - spring - end ------\n"); } /** * test - getClazzSetWithAnnoTest(), */ @SuppressWarnings("deprecation") @Test public void testGetClazzSetWithAnno() { System.out.println("------ annotation scan - reflections lib - start ------"); // get classes with in package, and has specified annotation, Set<Class<? extends Object>> clazzWithAnnoSet = PkgAnnoUtil.getClazzSetWithAnno(pkg, annoClazz); assertEquals(clazzWithAnnoSet.size(), annotatedClassCount); try { // execute a specific method, for (Class<? extends Object> clazz : clazzWithAnnoSet) { Method md = clazz.getMethod(methodName); if (md != null) { md.invoke(clazz.newInstance()); } } } catch (Exception e) { e.printStackTrace(); } System.out.println("------ annotation scan - reflections lib - end ------\n"); } }
相关推荐
Delete the folder under the specified path
mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
* Constructs a new instance with the specified detail message and cause. The * concrete handler is its super class. This constructor always used to construct * an exception wrapping the exist ...
调整参数后引起,ORA-00838: Specified value of MEMORY_TARGET is too small, needs to be at least 3 2768M 的解决方案
解决No input file specified
IllegalStateException: The specified child already has a parent.我的博客中有文章讲解
-sd <File> <ImageFile> Modify setup utility default setting with specified file on image file. -gstr <ImageFile> <File> -s|-b Read image file, and generate setup(-s) or setup browser(-b) string ...
解决Mysql Error-The user specified as a definer (‘mysql.infoschema’@’localhost’) does not exist
当某个数据库用户在数据库中插入、更新、删除一个表的数据,或者增加一个表的主键时或者表的索引时,常常会出现ora-00054:resource busy and acquire with nowait specified这样的错误。 主要是因为有事务正在执行...
提示No input file specified的解决方法.docx
Oracle10G控制台解决办法-Io 异常:Unknown host specified解决方法
需要提供给脚本MCC、MNC、LAC、CELLID信息,也可提供LAC或CELL的区间 信息,进行扫描。 使用说明: Usage: ./getlocal.sh --mcc={MCC} --mnc={MNC} --... --tcid: scan the CID and end with the specified number
Step1: Execute recompile.exe to generate specified version related folder and files (Recompile all package first, then Change Language to Chinese or othor one). step2: Execute @DelUnUsedFiles.bat to ...
nginx提示No input file specified怎么办.docx
specified virtual disk needs repair。附修复工具和使用方法。 分数不够的可以到VM官方下载修复工具,如果找不到就贡献1分下载我上传的吧:) =====================================================================...
You find several approaches to optimum phased array design, allowing you to provide specified array gain in a specific region of scan, using a minimum number of expensive, controlled devices....
loadlibrary failed with error 126:找不到指定模块
represent highly variable object classes and achieves state-of-the-art results in the PASCAL object detection challenges. While deformable part models have become quite popular, their value had not ...
LM3S811芯片手册,本文档是中文版的,详细的介绍了LM3S芯片手册的信息。供开发人员参考。
一.IIS Noinput file specified 方法一:改PHP.ini中的doc_root行,打开ini文件注释掉此行,然后重启IIS 方法二: 请修改php.ini 找到 ; cgi.force_redirect = 1 去掉前面分号,把后面的1改为0 即 cgi....