Choose Controller Bean instance by RequestMapping

Tags: , ,

I want Spring to create 2 instances of FooController. Requests to /foo should be handled by one of the instances and requests to /bar should be handled by the other instance. I want something like the below, but of course @RequestMapping doesn’t work that way and also Spring gives me the ambiguous mapping error on FooController as well.

public class FooController {
  String name;
  public FooController(String name) { = name; }

public class FooControllerConfig {
  public FooController getFooFooController(){
    return new FooController("foo");
  public FooController getBarFooController(){
    return new FooController("bar");


Don’t try this at home. This code was performed by a bored, trained professional…

You can have multiple instances of the same controller class, each of which handles a different URL through the same or a different method in the controller. The only thing is, I don’t know how to do it with just annotations. The way I just did it was to dynamically register each request mapping at initialization time. The FooController becomes a prototype bean (defined with annotations) so you can have Spring instantiate it multiple times, once for each mapping

public class FooController {

    private String name;

    public FooController() {}

    public FooController(String name) { = name;

    public ResponseEntity<String> handleRequests() throws Exception {
        return new ResponseEntity<>("Yo: " + name + " " + this.hashCode(), HttpStatus.OK);

public class EndpointService {

    private BeanFactory beanFactory;

    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    public void addFooController(String urlPath, String name) throws NoSuchMethodException {

        RequestMappingInfo requestMappingInfo = RequestMappingInfo

                beanFactory.getBean(FooController.class, name),

    public void handleContextRefreshEvent(ContextRefreshedEvent ctxStartEvt) {
        try {
            addFooController("/blah1", "blahblah1");
            addFooController("/blah2", "blahblah2");
            addFooController("/blah3", "blahblah3");
        } catch (NoSuchMethodException e) {


http://localhost:8080/blah1 returns: Yo: blahblah1 1391627345

http://localhost:8080/blah3 returns: Yo: blahblah3 2078995154

Source: stackoverflow