Is there a way to get a list of all Controllers, which were annotated with @Controller
? I would like to use them like:
@Autowired
public void addAll(List<Controller> controllers) throws Exception {
for (Controller controller : controllers) {
...
}
}
Thanks!
The @Controller annotation indicates that a particular class serves the role of a controller. There is no need to extend any controller base class or reference the Servlet API.
In simple words, @RestController is @Controller + @ResponseBody. So if you are using latest spring version you can directly replace @Controller with @RestController without any issue.
In Spring Boot, the controller class is responsible for processing incoming REST API requests, preparing a model, and returning the view to be rendered as a response. The controller classes in Spring are annotated either by the @Controller or the @RestController annotation.
There is no difference between @Component , @Service , @Controller , @Repository . @Component is the Generic annotation to represent the component of our MVC.
getBeansWithAnnotation()
If you have annotated them with controller ... :
@Autowired
private ListableBeanFactory listableBeanFactory;
then
Map<String, Object> controllers;
controllers = listableBeanFactory.getBeansWithAnnotation(Controller.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With