spring中AOP的四种常用通知,分别是
前置通知:在切入点方法执行前执行的方法
后置通知:在切入点方法执行后执行的方法
异常通知:在切入点方法动态代理中发生错误时执行的方法
最终通知:在切入点方法动态代理结束后执行的方法
代码演示
首先是创建一个UserService的接口和实现类
public interface UserService { public void save(); public void update(); public int delete(int i); }
public class UserServiceImpl implements UserService { //模拟保存 public void save() { System.out.println("保存方法运行了"); int a = 1/0; } //模拟更新 public void update() { System.out.println("更新方法运行了"); } //模拟删除 public int delete(int i) { System.out.println("删除方法运行了"); return 0; } }
定义一个日志类,包含4类的通知方法
public class Logger { public void BeforePrintLog(){ System.out.println("前置通知"); } public void AfterPrintLog(){ System.out.println("后置通知"); } public void ErrorPrintLog(){ System.out.println("异常通知"); } public void FinalPrintLog(){ System.out.println("最终通知"); } }
然后在bean.xml进行相关的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="UserServicr" class="com.itylm.service.impl.UserServiceImpl"></bean> <bean id="Logger" class="com.itylm.util.Logger"></bean> <aop:config> <aop:pointcut id="pt1" expression="execution( * com.itylm.service.impl.*.*(..))"/> <aop:aspect id="logAdvice" ref="Logger"> <aop:before method="BeforePrintLog" pointcut-ref="pt1"></aop:before> <aop:after-returning method="AfterPrintLog" pointcut-ref="pt1"></aop:after-returning> <aop:after-throwing method="ErrorPrintLog" pointcut-ref="pt1"></aop:after-throwing> <aop:after method="FinalPrintLog" pointcut-ref="pt1"></aop:after> </aop:aspect> </aop:config> </beans>
这里先是配置了两个bean
<bean id="UserServicr" class="com.itylm.service.impl.UserServiceImpl"></bean>
<bean id="Logger" class="com.itylm.util.Logger"></bean>
然后开始配置AOP
首先是切入点表达式另外一种配置方式
<aop:pointcut id="pt1" expression="execution( * com.itylm.service.impl.*.*(..))"/>
开始配置通知
<aop:aspect id="logAdvice" ref="Logger"> <!-- 配置前置通知:在切入点方法执行之前执行--> <aop:before method="BeforePrintLog" pointcut-ref="pt1"></aop:before> <!-- 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个--> <aop:after-returning method="AfterPrintLog" pointcut-ref="pt1"></aop:after-returning> <!-- 配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个--> <aop:after-throwing method="ErrorPrintLog" pointcut-ref="pt1"></aop:after-throwing> <!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行--> <aop:after method="FinalPrintLog" pointcut-ref="pt1"></aop:after> </aop:aspect>
测试类:
public class test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); UserService userService = (UserService) ac.getBean("UserServicr"); userService.save(); } }
运行结果: