title: JPA's context injection
body: <p>I'm trying to use JPA's Context Injection. Currently my code fails to have the context injected, throwing a NullPointerException:</p>
  
  <blockquote>
    <p>Exception in thread "main"
    java.lang.NullPointerException at
    learn.spring.dao.ListingDao.findById(ListingDao.java:19)</p>
  </blockquote>
  
  <p>Below is my code, anyone see what is wrong/missing? Thanks.</p>
  
  <p><strong>Spring Configuration xml:</strong></p>
  
  <pre><code>&lt;beans ....&gt;
  
  &lt;tx:annotation-driven /&gt;
  &lt;context:annotation-config /&gt;
  &lt;aop:aspectj-autoproxy /&gt;
  
  &lt;bean id="listingDao" class="learn.spring.dao.ListingDao" /&gt;
  
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
   &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
   &lt;property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx/xxxxx" /&gt;
   &lt;property name="username" value="xxxxxx" /&gt;
   &lt;property name="password" value="xxxxxx" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
   &lt;property name="persistenceUnitName" value="learnSpring" /&gt;
   &lt;property name="dataSource" ref="dataSource" /&gt;
   &lt;property name="jpaVendorAdapter"&gt;
    &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
     &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt;
     &lt;property name="showSql" value="true" /&gt;
     &lt;property name="generateDdl" value="true" /&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;/beans&gt;
  </code></pre>
  
  <p><strong>DAO class:</strong></p>
  
  <pre><code>package learn.spring.dao;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  import org.springframework.transaction.annotation.Transactional;
  
  import learn.spring.model.Registration;
  
  @Transactional
  public class ListingDao {
   public ListingDao() {}
  
   @PersistenceContext
   private EntityManager entityManager;
  
   public Registration findById(int id) {
    Query query = entityManager.createQuery("FROM Registration r WHERE r.memberId = ?1");
    query.setParameter(1, id);
    return (Registration) query.getSingleResult();
   }
  }
  </code></pre>
  
  <p><strong>peristence.xml file:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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="learnSpring" /&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>Class w/ "main" method:</strong></p>
  
  <pre><code>public class Performance {
  
   public static void main(String[] args){
    ApplicationContext context = new ClassPathXmlApplicationContext("learn-spring.xml");
    Performer performer = (Performer) context.getBean("musician");
    performer.perform();
    ListingDao listingDao = new ListingDao();
    Registration registration = listingDao.findById(1);
    System.out.println("Member id = " + registration.getMemberId() + " Name = " + registration.getFirstName() + " " + registration.getLastName());
   }
  
  }
  </code></pre>