Announcement Announcement Module
No announcement yet.
Multiple Instances of Annotated Controllers Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple Instances of Annotated Controllers

    I want to instantiate two instances of the same annotated controller. The two controllers will have different URLs and some different data (different success view).

    How can I get this to work? And how would I define the URLs?

    If I put @Controller on the class, the component-scan picks it up (once). If I remove @Controller, and define the controllers in XML, with the different parameterized data, I get a "Does your handler implement a supported interface like Controller?" error.

    I've defined "DefaultAnnotationHandlerMapping" and "SimpleControllerHandlerAdapter" in my XML context (along with <mvc:annotation-driven/>).

  • #2
    Ideally, controllers are singleton beans. I would use the @Controller annotation and let the framework instantiate the controller as a singleton.

    There are a few options to do what it sounds like you are trying to do - the choice depends on what exactly is different between the two code paths you are trying to create.

    URLs are mapped to handler methods of the controller, not to the entire class (you can apply @RequestMapping at the class level, but you can also apply it to methods to further clarify the mapping). So, one option would be to have two methods in the controller for the two URLs you want to handle, each returning the appropriate view data.

    If the differences between the two code paths can't be handled with separate methods in the same controller class, then you could create a base controller class and two sub-classes. Common code would of course go in the base class and the differences (URL mapping and view data setup) would go in the sub-classes.