AOP
在xml中的配置
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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="txManger" class="cn.jiedada._06xmlaop.TxManger"></bean> <bean id="userService" class="cn.jiedada._06xmlaop.service.impl.UserServiceImpl"></bean> <aop:config> <!--想要增强的类,切点设置--> <aop:pointcut id="pointcut" expression="execution(* cn.jiedada._06xmlaop.service.I*Service.*(..))"></aop:pointcut> <aop:aspect ref="txManger"> <!-- <aop:before method="begin" pointcut-ref="pointcut"></aop:before> <aop:after-returning method="commit" pointcut-ref="pointcut"></aop:after-returning> <aop:after-throwing method="rollback" pointcut-ref="pointcut"></aop:after-throwing> <aop:after method="close" pointcut-ref="pointcut"></aop:after>--> <!--增强方法--> <aop:around method="around" pointcut-ref="pointcut"></aop:around> </aop:aspect> </aop:config> </beans>
TxManger中的配置
这样就可以了

package cn.jiedada._06xmlaop; import org.aspectj.lang.ProceedingJoinPoint; public class TxManger { public void begin(){ System.out.println("开启事务"); } public void commit(){ System.out.println("提交事务"); } public void rollback(Throwable e){ System.out.println("回滚事务"+e.getMessage()); } public void close(){ System.out.println("关闭事务"); } public void around(ProceedingJoinPoint proceedingJoinPoint){ try { begin(); //获得传入的方法名 proceedingJoinPoint.proceed(); commit(); }catch (Throwable e){ rollback(e); }finally { close(); } } }
这只是一个模板