title: Is PersistenceAnnotationBeanPostProcessor of any use at all?
body: <p>According to its JavaDoc, PersistenceAnnotationBeanPostProcessor seems to be responsible for injecting the EntityManager with the annotation @PersistenceContext. It appears to imply without this bean declared in the Spring application context xml, the @PersistenceContext annotation won't work. </p>
  
  <p>However, based on my experiments, this is not the truth. </p>
  
  <h2>Persistence.xml</h2>
  
  <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="default" transaction-type="RESOURCE_LOCAL" /&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <h2>Spring application context XML</h2>
  
  <pre><code>&lt;context:component-scan base-package="com.test.dao" /&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="persistenceUnitName" value="default"/&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="true"/&gt;
              &lt;property name="generateDdl" value="true"/&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/&gt;
      &lt;property name="url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb"/&gt;
      &lt;property name="username" value="APP"/&gt;
      &lt;property name="password" value="APP"/&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven/&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- 
      &lt;bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
   --&gt;
  </code></pre>
  
  <h2>UserDaoImpl</h2>
  
  <pre><code>@Repository("userDao")
  public class UserDaoImpl implements UserDao {
  
      @PersistenceContext
      protected EntityManager entityManager;
  
      @Transactional
      public void save(User user) {
              entityManager.persist(user);
      }
  }
  </code></pre>
  
  <p>Whether I comment or uncomment the persistenceAnnotation bean, the result is the same. It doesn't hurt to leave the bean around, but what's the use of this bean?</p>
  
  <p>I am using Spring 3.0.5. </p>
  
  <p>Could someone provide a scenario where taking out this bean will result in failure?</p>
  
  <p>Also I am not fond of creating an empty persistence unit just to fool Spring. Luckily this problem has been addressed in Spring 3.1.0. </p>