Spring è un ottimo Inversion of Control (IoC) container. Ecco come fare ad eseguire uno scanning delle classi al fine di trovare tutte quelle che hanno una certa annotation
String basePackage = "it.lorenzoingrilli.examples";
LinkedList> list = new LinkedList>();
ClassPathScanningCandidateComponentProvider cp = new ClassPathScanningCandidateComponentProvider(false);
cp.addIncludeFilter(new AnnotationTypeFilter(AnnotationDaCercare.class));
for(BeanDefinition candidate : cp.findCandidateComponents(basePackage)) {
try {
Class> clazz = Class.forName(candidate.getBeanClassName());
list.add(clazz);
} catch (Throwable ex) {
ex.printStackTrace();
}
}
alla fine del ciclo nella collection list ci saranno tutte le classi con l'annotation specificata.