What’s the use of Java – persistenceannotation beanpostprocessor?
According to its Javadoc, persistenceannotation beanpostprocessor seems to be responsible for injecting annotation @ persistencecontext. Into entitymanager This seems to mean that the @ persistencecontext annotation will not work without this bean declared in the spring application context XML
However, according to my experiment, this is not true
persistence. In XML
<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">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL" />
</persistence>
Spring application context XML
<context:component-scan base-package="com.test.dao" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="default"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showsql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb"/>
<property name="username" value="APP"/>
<property name="password" value="APP"/>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!--
<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
-->
In userdaoimpl
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@PersistenceContext
protected EntityManager entityManager;
@Transactional
public void save(User user) {
entityManager.persist(user);
}
}
Whether I comment or uncomment the persistenceannotation bean, the result is the same Leaving this bean won't hurt it, but what's the use of this bean?
I use spring 3.0 five
Will someone provide a scenario where fetching the bean will lead to failure?
In addition, I don't like to create an empty persistence unit to fool spring Fortunately, this problem is already in spring 3.1 Resolved in 0
Solution
Persistenceannotationbeanpostprocessor activates components transparently by < context: component scan / > Specifically, it is the < context: annotation config / > element, which activates the bean, but this element is called < context: component scan / & gt Activate transparently
