Spring 掌管JPA事务是最好的,我总结出来以下是由SPRING注入JPA
<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 注释有效 -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean>
<!-- 注入entityManager -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="unitJPA"></property>
</bean>
<!-- 定义事务 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 注释让事务有效 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 定义使用事务的方法 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 定义一个切入点,使事务在它的切入点内有效 -->
<aop:config>
<aop:pointcut expression="execution(* com.ejb.bean.*Impl.*(..))" id="pointcut"/>
<aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice"/>
</aop:config>
//会话bean 的 Ioc
<bean id="stuService" class="com.ejb.bean.StuServiceImpl">
</bean>
</beans>