title: JPA is not Autowiring
body: <p>I'm getting a null pointer exception when I try to autowire my DAO object. Let me show you the code (I'm using maven + hibernate + spring + jpa):</p>
  
  <pre><code>@Repository
  public class DAOFriendImp implements DAOFriend{
  
      private EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
      private EntityManager entityManager = emf.createEntityManager();
  
      @PersistenceContext(unitName = "persistence")
      public void setEntityManager(EntityManager entityManager) {
          this.entityManager = entityManager;
      }
  
      @Override
      public boolean save(Person person, Person friend) {
          throw new UnsupportedOperationException("Not supported yet.");
      }
  
      @Override
      public List&lt;Person&gt; getAllFriendOfPersonWithId(int id) {
          Person person = entityManager.find(Person.class, id);
          return person.getFriends();
      }
  
      @Override
      public boolean delete(Person person, Person friend) {
          throw new UnsupportedOperationException("Not supported yet.");
      }
  
  }
  </code></pre>
  
  <p>As you can see I'm initializing my entityManager manually. That's because if I use the persistenceContext I get a null pointer exception too. Perhaps because the DAOFriendImp is not getting autowires properly? I don't really know.</p>
  
  <p>Now let me show you where I get the null pointer:</p>
  
  <pre><code>public class FriendListLogic {
  
  @Autowired
  DAOFriendImp dao;
  
  public List&lt;Person&gt; getEntrys(int userId) {
      List&lt;Person&gt; result = dao.getAllFriendOfPersonWithId(userId); //here
      return result;
  }
  }
  </code></pre>
  
  <p>I don't know why it's not getting injected! Everything seems to be alright in the configuration XML. I will add the rest of the files (pom, persistence, applicationcontext and servlet.xml)</p>
  
  <p>Persistence.xml:</p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
      version="1.0"&gt;
  
  &lt;persistence-unit name="persistence" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;class&gt;com.divux.onex.model.Person&lt;/class&gt;
      &lt;class&gt;com.divux.onex.model.CircleOfConnection&lt;/class&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
          &lt;property name="javax.persistence.jdbc.driver"   value="com.mysql.jdbc.Driver"   /&gt;
          &lt;property name="javax.persistence.jdbc.url"        value="jdbc:mysql://localhost:3306/onex" /&gt;
          &lt;property name="javax.persistence.jdbc.user"     value="root" /&gt;
          &lt;property name="javax.persistence.jdbc.password" value="bterra" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Note: I have my jdbc user password driver and url here because I had problems in the dataSource bean in the applicationContext ... it just didn't work if I had this info there (don't know why)</p>
  
  <p>applicationContext.xml </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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;context:annotation-config /&gt;
  &lt;bean  class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt; 
  &lt;bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entityManagerFactory"   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="persistenceUnitName" value="persistence" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="database" value="MYSQL" /&gt;
              &lt;property name="showSql" value="false" /&gt; 
              &lt;property name="generateDdl" value="false" /&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"  /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="loadTimeWeaver"&gt;  
          &lt;bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"&gt;&lt;/bean&gt;  
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"&gt;&lt;/bean&gt;  
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>servlet.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
  &lt;context:component-scan base-package="com.divux.onex.controller"&gt;&lt;/context:component-scan&gt;
  &lt;context:component-scan base-package="com.divux.onex.daoImp"&gt;&lt;/context:component-scan&gt;
  
  &lt;context:property-placeholder location="classpath*:resources/properties/*.properties"/&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
      &lt;property name="suffix" value=".jsp"/&gt;
  &lt;/bean&gt;
  
  &lt;mvc:annotation-driven/&gt;
  &lt;mvc:view-controller path="/" view-name="index"/&gt;
  &lt;mvc:resources mapping="/resources/**" location="/resources/"/&gt;
  
    &lt;import resource="applicationContext.xml" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>and finally the pom.xml (just a part of it)</p>
  
  <pre><code>&lt;properties&gt;
      &lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;org.springframework.version&gt;3.0.5.RELEASE&lt;/org.springframework.version&gt;
      &lt;aspectj.version&gt;1.6.11&lt;/aspectj.version&gt;
      &lt;org.slf4j-version&gt;1.5.8&lt;/org.slf4j-version&gt;
      &lt;hibernate.version&gt;3.6.7.Final&lt;/hibernate.version&gt;
      &lt;hibernate-search.version&gt;4.0.0.Final&lt;/hibernate-search.version&gt;
  &lt;/properties&gt;
  &lt;!-- JPA with Hibernate Persistence --&gt;             
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;5.1.16&lt;/version&gt;
      &lt;/dependency&gt;
     &lt;!-- Persistance --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;cglib&lt;/groupId&gt;
                  &lt;artifactId&gt;cglib&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;dom4j&lt;/groupId&gt;
                  &lt;artifactId&gt;dom4j&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-search&lt;/artifactId&gt;
          &lt;version&gt;${hibernate-search.version}&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;!-- &lt;dependency&gt; &lt;groupId&gt;org.hibernate&lt;/groupId&gt; &lt;artifactId&gt;hibernate-jmx&lt;/artifactId&gt; 
  &lt;version&gt;${hibernate-jmx.version}&lt;/version&gt; &lt;/dependency&gt; --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpamodelgen&lt;/artifactId&gt;
          &lt;version&gt;1.1.1.Final&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
  &lt;!-- only need at compile time --&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;cglib&lt;/groupId&gt;
          &lt;artifactId&gt;cglib-nodep&lt;/artifactId&gt;
          &lt;version&gt;2.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.transaction&lt;/groupId&gt;
          &lt;artifactId&gt;jta&lt;/artifactId&gt;
          &lt;version&gt;1.1&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  &lt;!-- validation --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;version&gt;4.1.0.Final&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
                  &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
                  &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.validation&lt;/groupId&gt;
          &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.0.GA&lt;/version&gt;
      &lt;/dependency&gt;
  
  &lt;!-- AspectJ --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
          &lt;version&gt;${aspectj.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
          &lt;version&gt;${aspectj.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Let me know if you need more information! I will not add the error output because I think it's unnecessary. If you need it let me know. </p>