Vojtěch Vojtěch - 5 months ago 13
Java Question

Getting all annotated controllers with @Controller

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!

Answer

getBeansWithAnnotation()

If you have annotated them with controller ... :

@Autowired
private ListableBeanFactory listableBeanFactory;

then

Map<String, Object> controllers;
controllers = listableBeanFactory.getBeansWithAnnotation(Controller.class);