title: Implementing a reusable @Controller
body: <p>I'm implementing <a href="https://github.com/alexpdp7/jcrud" rel="nofollow noreferrer">some SQL-CRUD components for Spring-MVC</a> and I'm facing a problem.</p>
  
  <p>I provide a <a href="https://github.com/alexpdp7/jcrud/blob/master/jcrud/src/main/java/net/pdp7/jcrud/TableController.java" rel="nofollow noreferrer"><code>TableController</code></a> which provides several controller methods:</p>
  
  <pre><code>public class TableController ... {
  
      public TableController(String tableName) { ... }
  
      @RequestMapping("/list")
      public ModelAndView list() { ... }
  
      @RequestMapping("/addForm")
      public ModelAndView addForm() { ... }
      ...
  </code></pre>
  
  <p>This <code>TableController</code> basically provides functionality over a database table- a controller method that lists the table's rows, forms for creating and updating table rows, etc. It is a somewhat complex class, as it needs to be customizable to offer different behaviour for different tables.</p>
  
  <p>Ideally, I would like users of this controller to be able to add a new instance for each database table they want to expose in their website by adding it to a context, something like this:</p>
  
  <pre><code>@Configuration
  class Context {
      @Bean("/vets/")
      public TableController vetController() {
          return new TableController("vets");
      }
  
      @Bean("/pets/")
      public TableController petController() {
          return new TableController("pets");
      }
  </code></pre>
  
  <p>I would expect then to get <code>/vets/list</code>, <code>/vets/addForm</code>, <code>/pets/list</code> URLs exposed in their app.</p>
  
  <p>However, this does not work. The only way I've gotten this to work is by not using <em>any</em> <code>@RequestMapping</code> annotation in <code>TableController</code> and having the apps extend <code>TableController</code> for each table they want to expose and define <em>all</em> <code>@RequestMapping</code>s in the subclasses, what I'm doing for instance in <a href="https://github.com/alexpdp7/jcrud/blob/master/petclinic/src/main/java/net/pdp7/jcrud/petclinic/Application.java" rel="nofollow noreferrer"><code>Application</code></a> and <a href="https://github.com/alexpdp7/jcrud/blob/master/petclinic/src/main/java/net/pdp7/jcrud/petclinic/PetController.java" rel="nofollow noreferrer"><code>PetController</code></a></p>
  
  <p>This works but it's extremely awkward. Any way to improve this?</p>
  
  <p>Edit: it seems that I'm fucked:</p>
  
  <blockquote>
    <p>There are also several things no longer possible:</p>
    
    <ul>
    <li>Select a controller first with a SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping and then narrow the method based on
    @RequestMapping annotations.</li>
    </ul>
  </blockquote>
  
  <p><a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-31-vs-30" rel="nofollow noreferrer">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-31-vs-30</a></p>