To provide some runtime generated API documentation I want to iterate over all Spring MVC controllers. All controllers are annotated with the Spring @Controller annotation.
I have also came across such requirement before some months and I have achieved it using the following code snippet.
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
for (BeanDefinition beanDefinition : scanner.findCandidateComponents("com.xxx.yyy.controllers")){
System.out.println(beanDefinition.getBeanClassName());
}
You can also do something like this with your controllers.
Updated the code snippet. Removed the not necessary code and just displaying the class name of the controllers for better understanding. Hope this helps you. Cheers.