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. Currently I do it like this:
for (final Object bean: this.context.getBeansWithAnnotation(
Controller.class).values())
{
...Generate controller documentation for the bean...
}
But the first call of this code is EXTREMELY slow. I wonder if Spring iterates over ALL classes in the classpath instead of just checking the defined beans. The controllers are already loaded when the above code is run, the log displays all of them with their request mappings so Spring MVC must already know them all and there must be a faster way to get a list of them. But how?
I have also came across such requirement before some months and I have achieved it using the following code snippet.
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.