查找某注解修饰的所有类使用 ClassPathScanningCandidateComponentProvider 进行扫描。
查找某注解修饰的方法,先找到那个类,然后得到所有的方法,使用AnnotatedElementUtils.hasAnnotation判断方法是否被某注解修饰即可
下面是一个简单的例子
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(DockingAnnotation.class));
Set candidates = provider.findCandidateComponents(“package_name”);
for (BeanDefinition definition : candidates){
try {
Class clz = Class.forName(definition.getBeanClassName());
Method[] methods = clz.getMethods();
for (Method method : methods){
if (AnnotatedElementUtils.hasAnnotation(method,DockIngMessage.class)){
DockIngMessage dockIngMessage = AnnotatedElementUtils.getMergedAnnotation(method,DockIngMessage.class);
System.out.println(dockIngMessage.name());
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}