1.配置核心事物管理器
<!-- 核心事物配置 AOP -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
2.在applicationContext.xml中配置AOP事物
(1)配置通知
<tx:advice id="txAdvice"
transaction-manager="transactionManager"
<tx:attributes>
<tx:method name="save*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ"
propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
(2)配置將通知織入目標
<aop:config>
<!--配置切點-->
<aop:pointcut expression="execution(* service.impl.*ServiceImpl.*(..))" id="txPc" />
<!--配置切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
三.註解配置AOP事物
四.service類中使用註解
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class UserServiceImpl implements UserService{
在方法中定義,則只指定當前方法
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void saveUser(User u) {
ud.save(u);
}