title: Why does Spring's @Configurable sometimes work and sometimes not?
body: <p>I'm trying to use automatic dependency injection via Spring's @Configurable annotation w/ @Resource on the fields needing injection. This involved some setup, like passing spring-agent.jar to my JVM. For the full details <a href="http://static.springframework.org/spring/docs/2.5.x/reference/aop.html" rel="noreferrer">see here</a>.</p>
  
  <p>It works... mostly. When my Tomcat is booting up, I see the AspectJ init messages, my User objects automatically get FileService references, etc.</p>
  
  <p>The problem is that sometimes it just doesn't happen. It appears to be completely random; sometimes I boot up and the dependencies are not injected, sometimes they are. I previously had trouble with @Transactional being on my User because it created a conflict, I believe with proxies. I am using JPA, so my User is marked with @Entity, so my best guess right now is that this is creating a conflict. I've read you can't auto proxy a proxy. To offset the conflict, I followed some notes I found online about excluding <strong>CGLIB</strong> and <strong>javassist</strong> which Hibernate (my JPA impl) uses.</p>
  
  <p>Clues: </p>
  
  <ul>
  <li>It's all or nothing. All of my @Configurable instances have been injected or none of them.</li>
  <li>Reloading (reinstantiating) the Entity from the DB doesn't appear to help; it's either working or not.</li>
  <li>Rebooting Tomcat any number of time also won't even fix it. The only thing that appears to roll the dice again is a redeploy. In other words, if I redeploy it may work.</li>
  </ul>
  
  <p>How can I figure out what is going wrong? Is anyone using @Configurable with JPA? Why isn't my <em>dependencyCheck = true</em> throwing an error when dependencies are not actually injected?</p>
  
  <p><strong>Entity</strong></p>
  
  <pre><code>@Entity
  @Configurable(dependencyCheck = true)
  @NamedQueries( { @NamedQuery(name = "User.findAll", query = "SELECT user FROM User user"),
      @NamedQuery(name = "User.findByEmail", query = "SELECT user FROM User user WHERE user.email = :email") })
  public abstract class User extends BaseModel {
  
  private static final long serialVersionUID = 7881431079061750040L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  private Long id;
  
  @Column(unique = true, nullable = false)
  private String email;
  
  @Basic(optional = false)
  private String password;
  
  @Resource
  private transient UserEmailer userEmailer;
  
  @Resource
  private transient FileService fileService;
  
  ...
  </code></pre>
  
  <p><strong>aop.xml</strong></p>
  
  <pre><code>&lt;!DOCTYPE aspectj PUBLIC
      "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"&gt;
  &lt;aspectj&gt;
      &lt;weaver options="-verbose"&gt;
          &lt;include within="com.myapp.domain..*" /&gt;
          &lt;exclude within="*..*CGLIB*" /&gt;
          &lt;exclude within="*..*javassist*" /&gt;
      &lt;/weaver&gt;
      &lt;aspects&gt;
          &lt;aspect name="org.springframework.beans.factory.aspectj.AbstractInterfaceDrivenDependencyInjectionAspect" /&gt;
      &lt;/aspects&gt;
  &lt;/aspectj&gt;
  </code></pre>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>...
  
  &lt;context:spring-configured /&gt;
  
  &lt;context:load-time-weaver /&gt;
  
  &lt;context:component-scan base-package="com.myapp" /&gt;
  
  ...
  </code></pre>