PersistenceAnnotationBeanPostProcessor是否可以使用? [英] Is PersistenceAnnotationBeanPostProcessor of any use at all?
问题描述
根据其JavaDoc,PersistenceAnnotationBeanPostProcessor似乎负责使用注释@PersistenceContext注入EntityManager。它似乎暗示如果没有在Spring应用程序上下文xml中声明这个bean,@ PerersContext注释将不起作用。
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.
然而,根据我的实验,这不是事实。
However, based on my experiments, this is not the truth.
<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应用程序上下文XML
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" />
-->
UserDaoImpl
UserDaoImpl
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@PersistenceContext
protected EntityManager entityManager;
@Transactional
public void save(User user) {
entityManager.persist(user);
}
}
无论是评论还是取消注释persistenceAnnotation bean,结果是一样的。离开bean并没有什么坏处,但是这个bean的用途是什么?
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?
我使用的是Spring 3.0.5。
I am using Spring 3.0.5.
有人会提供一个场景,取出这个bean会导致失败吗?
Could someone provide a scenario where taking out this bean will result in failure?
我也不喜欢创建一个空的持久性单元只是为了愚弄Spring。幸运的是,这个问题已在Spring 3.1.0中得到解决。
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.
推荐答案
透明地激活
元素。确切地说,它是激活bean的 PersistenceAnnotationBeanPostProcessor
< context:component-scan /> < context:annotation-config />
元素,但是这个元素又被<透明地激活了。上下文:component-scan />
。
The PersistenceAnnotationBeanPostProcessor
transparently activated by the <context:component-scan />
element. To be precise it's the <context:annotation-config />
element that activates the bean but this element in turn gets transparently activated by <context:component-scan />
.
这篇关于PersistenceAnnotationBeanPostProcessor是否可以使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!