title: @Autowired inside @Entity not working properly
body: <p>When I log in to my applications I get a <code>NullPointerException</code> at an <code>@Autowired</code> field <code>hashProvider</code>. The strange thing is, it happens only sometimes! When I tried the debug mode, the field was autowired, when I run it normally, it was not.</p>
  
  <pre><code>@Configurable(preConstruction=true)
  @Entity
  @Table(name="DB_USER")
  @Inheritance(strategy = InheritanceType.JOINED)
  @DiscriminatorColumn(name = "USER_TYPE", discriminatorType = DiscriminatorType.STRING)
  public abstract class User extends AbstractEntity {
  
       //...
  
      @Autowired(required = true)
      private transient HashProvider hashProvider; 
  
       //...
  
      public boolean hasPassword(String password){
          LOG.info("is null "+(hashProvider==null));   //sometimes is null sometimes not
          return hashProvider.computeHash(password + salt).equals(this.password);  //null pointer
      }
  }
  
  @Component("hashProvider")
  public class SHA1Provider implements HashProvider{
      //...
      @Override
      public String computeHash(String s) {
          //...
      }
  }
  </code></pre>
  
  <p>Also logging as different users throws the exception or not. While logging in as <code>Doctor</code>, the field is null, as <code>AdminUser</code>, the field is not</p>
  
  <pre><code>@Entity
  public class AdminUser extends User {
  
      @Override
      public String toString() {
          return super.toString() + "AdminUser{" + '}';
      }
  
      @Override
      public Boolean isAdmin() {
          return true;
      }    
  }
  
  @Entity
  @DiscriminatorValue("doctor")
  public class Doctor extends User {
  
      private String address;
  
      @Column(length = 10, unique = true, name = "DOCTOR_BCN", nullable = false)
      private Long birthNumber;
  
      private Integer phone;
  
      @OneToMany(mappedBy = "doctor")
      @OrderBy("name ASC")
      private List&lt;Patient&gt; patients;
  
      //setters and getters       
  
      @Override
      public String toString() {
          return super.toString() + "Doctor{}";
      }
  
      @Override
      public Boolean isAdmin() {
          return false;
      }
  }
  </code></pre>
  
  <p>This is the applicationContext.xml file:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
          &gt;
      &lt;!-- Use annotation to configure Spring context --&gt;
      &lt;context:annotation-config /&gt;
      &lt;mvc:annotation-driven /&gt;
      &lt;!-- Search for beans under the com.wpa package (Using annotation @Component, @Repository, @Service) --&gt;
      &lt;context:component-scan base-package="com.wpa"/&gt;
      &lt;!-- @Configurable support --&gt;
      &lt;context:spring-configured/&gt;
      &lt;!-- Property files --&gt;
      &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="locations"&gt;
              &lt;list&gt;
                  &lt;value&gt;/WEB-INF/properties/jdbc.properties&lt;/value&gt;
                  &lt;value&gt;/WEB-INF/properties/jpa.properties&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;!-- Connection pool --&gt;
      &lt;bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"&gt;
          &lt;property name="driverClass" value="${jdbc.driverClassName}"/&gt;
          &lt;property name="jdbcUrl" value="${jdbc.url}"/&gt;
          &lt;property name="username" value="${jdbc.username}"/&gt;
          &lt;property name="password" value="${jdbc.password}"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- This defines the EntityManagerFactory bean, which provides the application with EntityManager instances = persistence context --&gt;
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"&gt;
                  &lt;property name="databasePlatform" value="${jpa.platform}"/&gt;
                  &lt;property name="generateDdl" value="true"/&gt;
                  &lt;property name="showSql" value="true"/&gt;
              &lt;/bean&gt; 
          &lt;/property&gt;
          &lt;property name="jpaPropertyMap"&gt;
              &lt;map&gt;
                  &lt;entry key="eclipselink.weaving" value="static"/&gt;
                  &lt;entry key="eclipselink.ddl-generation" value="create-or-extend-tables" /&gt;
              &lt;/map&gt;
          &lt;/property&gt;
          &lt;property name="packagesToScan" value="com.wpa"/&gt;
          &lt;property name="jtaDataSource" ref="dataSource"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Transaction manager for declarative transactional demarcation --&gt;
      &lt;bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Use the declared transaction manager to manage transaction using the @Transactional annotation --&gt;
      &lt;!-- proxy-target-class=true enables use of concrete classes without interfaces as beans with the @Transactional annotation --&gt;
      &lt;tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" /&gt;
          &lt;bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"&gt;
          &lt;property name="transactionManager"&gt;
              &lt;ref bean="txManager"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;   
  &lt;/beans&gt;
  </code></pre>