title: Spring autowiring using @Configurable
body: <p>I'm playing with the idea of using Spring <code>@Configurable</code> and <code>@Autowire</code> to inject DAOs into domain objects so that they do not need direct knowledge of the persistence layer.</p>
  
  <p>I'm trying to follow <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable" rel="noreferrer">http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable</a>, but my code seems to have no effect.</p>
  
  <p>Basically, I have:</p>
  
  <pre><code>@Configurable
  public class Artist {
  
      @Autowired
      private ArtistDAO artistDao;
  
      public void setArtistDao(ArtistDAO artistDao) {
          this.artistDao = artistDao;
      }
  
      public void save() {
          artistDao.save(this);
      }
  
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>public interface ArtistDAO {
  
      public void save(Artist artist);
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Component
  public class ArtistDAOImpl implements ArtistDAO {
  
      @Override
      public void save(Artist artist) {
          System.out.println("saving");
      }
  
  }
  </code></pre>
  
  <p>In application-context.xml, I have:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springsource.org/dtd/spring-beans-2.0.dtd"&gt;
  &lt;beans&gt;
  
      &lt;bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /&gt;
      &lt;bean class="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" factory-method="aspectOf"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Class path scanning and initialisation is performed by the spring module for Play! framework, although other autowired beans work, so I'm pretty sure this is not the root cause. I'm using Spring 3.0.5.</p>
  
  <p>In other code (inside a method in bean that's injected into my controller using Spring, in fact), I'm doing this:</p>
  
  <pre><code>Artist artist = new Artist();
  artist.save();
  </code></pre>
  
  <p>This gives me a NullPointerException trying to access the artistDao in Artist.save().</p>
  
  <p>Any idea what I'm doing wrong?</p>
  
  <p>Martin</p>