1.设置transactionManager
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
2.设置advice
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
3.接下来设置AOP
<aop:config>
<aop:pointcut id="businessService" expression="execution(* com.hisoft.db.hibernate.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
<aop:aspect id="businessAspect" ref="AOPTest">
<aop:before pointcut-ref="businessService" method="before"/>
<aop:after-returning pointcut-ref="businessService" method="after"/>
</aop:aspect>
</aop:config>
这个的意思是说,当执行到com.hisoft.db.hibernate.impl这个包下面的任何类的任何方法,而且不管参数是什么,也就是说这个包下面的所有方法调用了,都要接受前面的transactionManager的管理。
4.AOP设置
<bean id="AOPTest" class="com.wod.common.LogHandler.AOPTest"/>
<aop:config>
<aop:pointcut id="businessService" expression="execution(* com.hisoft.db.hibernate.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
<aop:aspect id="businessAspect" ref="AOPTest">
<aop:before pointcut-ref="businessService" method="before"/>
<aop:after-returning pointcut-ref="businessService" method="after"/>
</aop:aspect>
</aop:config>
定义一个切面,叫做businessAspect,引用的是我前面定义的一个叫做AOPTest的类,然后下面的两句话:
<aop:before pointcut-ref="businessService" method="before"/>
<aop:after-returning pointcut-ref="businessService" method="after"/>
aop:before 指的是在调用目标方法之前要干点事情,pointcut-ref="businessService"就是目标的方法,在调用匹配这个pointcut 的方法之前,会调用 method中定义的那个方法。

