title: Spring non singleton controller
body: <p>I am building a spring based application that handles server-side game management for a mobile game app. The stack is Spring/Hibernate/Jersey. I want the clients (mobile) to call a REST API to update/retrieve the game state.</p>
  
  <p>I created a <code>TournamentController</code> class which responsability is to update the tournament state using some business logic. This class is designed to be instanciated every time an operation on a Tournament is required and then thrown away.</p>
  
  <pre><code>public class TournamentController {
  
      @Autowired
      private TournamentDAO tournamentDAO;
  
      private final Tournament tournament;
  
      public TournamentController( Tournament tournament ) {
          this.tournament = tournament;
      }
  
      public void startTournament() {
          if ( tournament.getState() != TournamentState.SETUP ) {
              throw new TournamentAlreadyStartedException();
          }
  
          tournament.setState( TournamentState.IN_PROGRESS );
  
          //... some other logic and calls to other DAOs
  
          tournamentDAO.save( tournament );
      }
  
  }
  </code></pre>
  
  <p>I also created a <code>TournamentResource</code> class that is the REST front end for the tournament. It's responsability is to do some basic validation (user security, ...) and exception translation.</p>
  
  <pre><code>@Path( "/tournament" )
  @Component
  @Scope( "prototype" )
  public class TournamentResource {
  
      private static final Log log = LogFactory.getLog( TournamentResource.class );
  
      @Autowired
      private TournamentDAO tournamentDAO;
  
      @GET
      @Path( "{tournamentId}/start" )
      @Produces( MediaType.APPLICATION_JSON )
      @Transactional
      public TournamentDTO startTournament( @PathParam( "tournamentId" ) long tournamentId ) {
          Tournament tournament = tournamentDAO.getTournament( tournamentId, LockMode.PESSIMISTIC_WRITE );
          if ( tournament == null ) {
              throw new WebApplicationException( Status.NOT_FOUND );
          }
  
          try {
              TournamentController controller = new TournamentController( tournament );
              controller.startTournament();
  
          } catch ( TournamentAlreadyStartedException e ) {
              log.warn( "Could not start tournament " + tournamentId + " since it is already started." );
              throw new RestException( Status.BAD_REQUEST, "Tournament already started" );
          }
  
          return DTOConverterUtil.getTournament( tournament );
      }
  
  }
  </code></pre>
  
  <p>I am using load time weaving with AspectJ. Here is my context:</p>
  
  <pre><code>   &lt;context:annotation-config /&gt;
  
     &lt;!-- Make spring aware ANY pojo with the @Configurable annotation --&gt;
     &lt;context:spring-configured /&gt;
  
     &lt;!-- Scan all classes in com.mdarveau for annotations --&gt;
     &lt;context:component-scan base-package="so.question" /&gt;
  
     &lt;!-- Load Time Weaver --&gt;
     &lt;context:load-time-weaver weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" aspectj-weaving="on" /&gt;
  
     &lt;!-- DB config --&gt;
     &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
        ...
     &lt;/bean&gt;
  
     &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
        &lt;property name="dataSource" ref="dataSource" /&gt;
        &lt;property name="packagesToScan" value="com.mdarveau.fnp.model" /&gt;
        &lt;property name="hibernateProperties"&gt;
           &lt;value&gt;
              hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
           &lt;/value&gt;
        &lt;/property&gt;
     &lt;/bean&gt;
  
     &lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
        &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
     &lt;/bean&gt;
  
     &lt;!-- enable @Transactional --&gt;
     &lt;tx:annotation-driven transaction-manager="txManager" mode="aspectj" /&gt;
  </code></pre>
  
  <p>My <code>TournamentResource</code> class is a singleton by design and is working well.</p>
  
  <p>My problem is that when it instanciate the <code>TournamentController</code> using <code>new</code> instead of spring, it's <code>@Autowired</code> attributes does not seems to be wired correctly. I tried to add the <code>@Component</code> annotation on it without success.</p>
  
  <p>Should I make <code>TournamentResource</code> <code>ApplicationContextAware</code> and create the <code>TournamentController</code> through spring?</p>
  
  <p>This must be a fairly common problem. I see a lot of examples where the backend is a singleton but I would like to avoid passing the <code>Tournament</code> to every method call on <code>TournamentController</code> (all then to all private methods in <code>TournamentController</code>).</p>