/*
* AOP面向切面编程
* 切面
切点 泛指所有的点
连接点 一个具体的点
通知
* 前置通知-->save*()-->后置通知
* 1.首先根据切点进行对应方法的监控
* 2.然后当监控的方法真正执行的时候再来找到我们对应的通知,执行通知对应的方法
* 3.顺序为前置通知-->执行的方法-->后置通知
*/
<aop:config>
<aop:aspect ref="firstAop">
<!-- <aop:before method="befores" pointcut-ref="fristPoint"/>
<aop:after method="after" pointcut-ref="fristPoint"/>
<aop:after-returning method="returning" returning="result" pointcut-ref="fristPoint"/>
<aop:after-throwing method="throwing" throwing="ex" pointcut-ref="fristPoint"/> -->
<aop:around method="around" pointcut-ref="fristPoint"/>
<!-- 根据表达式来确定我们的方法的监控名单 -->
<aop:pointcut expression="execution(* com.lanou.service.impl.UserServiceImpl.*(..))" id="fristPoint"/>
</aop:aspect>
</aop:config>
public void around(ProceedingJoinPoint joinPoint) {
try {
System.out.println("前置通知");
Object result = joinPoint.proceed();
System.out.println("result:"+result);
System.out.println("后置返回通知");
} catch (Throwable e) {
// TODO Auto-generated catch block
System.out.println("后置异常通知");
e.printStackTrace();
}finally {
System.out.println("后置通知");
}
}
事务管理:
<!-- 事务管理 -->
<!-- 1.创建事务对象,将sessionfactory交给他管理
-->
<bean id="tx" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="advice" transaction-manager="tx">
<tx:attributes>
<!-- 设置为只读,将不再为其开启事务 -->
<tx:method name="select*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.lanou.dao.*.*(..))" id="txCut"/>
<aop:advisor advice-ref="advice" pointcut-ref="txCut"/>
</aop:config>