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"); } }
相关推荐
标题中的"S7-200通讯 specified access point not found补丁"指的是在使用Siemens S7-200系列PLC(可编程逻辑控制器)进行通信时遇到的问题,即"指定的访问点未找到"。这通常是由于网络配置错误、硬件故障或者软件不...
mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
IllegalStateException: The specified child already has a parent.我的博客中有文章讲解
* 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 ...
Nginx 提示 "No input file specified" 的解决方法 Nginx 是一个流行的开源 web 服务器软件,然而,在使用 Nginx 时,有时可能会遇到 "No input file specified" 的错误提示,这篇文章将详细讲解该错误的解决方法。...
"No input file specified解决方法" 在Web服务器中,"No input file specified"是常见的错误信息,出现该错误的原因有多种,例如PHP.ini文件配置不正确、Apache或Nginx服务器配置不当等。下面将从IIS、Apache和...
你可以参考提供的资源,如 "No input file specified的解决方法-百度经验.url" 和 "报错 ”No input file specified.“ 排查 - zh_mead的博客 - CSDN博客.url",这些链接可能提供了更具体的步骤和案例。 在实际操作...
在JavaScript的模块化开发中,`package.json` 文件起着至关重要的作用,它定义了项目的元数据,并指定了模块的入口文件。对于Vue.js项目,尤其是由`@vue/cli`生成的Vue3项目,`package.json`中的`main`、`module`和`...
_tprintf(_T("Failed to delete directory with error %d\n"), GetLastError()); else _tprintf(_T("Directory successfully deleted.\n")); return 0; } ``` 这段代码首先遍历指定目录下的所有文件和子目录,...
specified virtual disk needs repair。附修复工具和使用方法。 分数不够的可以到VM官方下载修复工具,如果找不到就贡献1分下载我上传的吧:) =====================================================================...
NX二次开发UF_CLONE_part_under_specified 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE 分析等领域的...
-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 ...
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 ...
解决 No input file specified 的方法 在编程中,我们经常会遇到 "No input file specified" 的提示,这个错误信息很容易让人感到困惑和沮丧。实际上,这个错误信息是由于服务器或编程语言的配置问题引起的。今天,...
为了添加XMLCatalog到你的plugin Specified Entries,你需要按照以下步骤操作: 1. 创建XMLCatalog配置文件:在你的项目中创建一个名为`catalog.xml`的文件,其中定义了URI到本地文件的映射。例如: ```xml ...
在MySQL数据库操作中,"The user specified as a definer (‘mysql.infoschema’@’localhost’) does not exist" 是一个常见的错误提示,这通常发生在尝试执行存储过程、触发器或者视图等对象时,这些对象的定义者...
Oracle数据库在处理并发事务时,可能会遇到“ORA-00054: resource busy and acquire with NOWAIT specified”错误,这通常意味着当前操作试图获取一个已被其他事务占用的资源,而该请求又指定了不允许等待(NOWAIT)...