aop-xml-环绕增强
xml代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id="mtm" class="cn.zj.spring.util.MyTransactionManager"></bean> <bean id="service" class="cn.ljs.spring.service.impl.UserServiceImpl"></bean> <aop:config> <aop:pointcut expression="execution(* cn.zj.spring..*.*(..))" id="exp"/> <aop:aspect ref="mtm"> <aop:around method="allMethod" pointcut-ref="exp"/> </aop:aspect> </aop:config> </beans>
工具类代码
package cn.ljs.spring.util; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Pointcut; public class MyTransactionManager { public void allMethod(ProceedingJoinPoint jp) { try { System.out.println("开始事务"); jp.proceed(); System.out.println("提交事务"); } catch (Throwable e) { // TODO Auto-generated catch block System.out.println("回滚事务"); e.printStackTrace(); } finally { System.out.println("关闭session"); } } }