一、UserService接口
<span style="font-size:18px;">package com.seven.spring.n_aop_xml; public interface UserService { void queryUsers(); void saveUser(); void deleteUser(); } </span>二、UserServiceImpl实现类
<span style="font-size:18px;">package com.seven.spring.n_aop_xml; public class UserServiceImpl implements UserService{ //这个方法是抛异常的 public void queryUsers() { System.out.println("查询一个User"); int i= 1/0; } public void saveUser() { System.out.println("保存一个User"); } public void deleteUser() { System.out.println("删除一个User"); } }</span>
<span style="font-size:18px;">三、applicationContext.xml中的配置</span>
<span style="font-size:18px;"><?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 配置UserService对象 --> <bean id="userService" class="com.seven.spring.n_aop_xml.UserServiceImpl"> </bean> <!-- 配置一个通知对象 --> <bean id="logAdvice" class="com.seven.spring.n_aop_xml.LogAdvice"></bean> <!-- AOP相关的配置都在aop:config中 --> <aop:config> <!-- 声明一个切面 --> <aop:aspect ref="logAdvice"> <!-- 声明一个切入点 --> <aop:pointcut expression="execution(public * *(..))" id="myPointcut" /> <!-- 指定某一个切入点执行的某操作 --> <aop:before method="before" pointcut-ref="myPointcut" /><!-- 前置通知 --> <aop:after method="after" pointcut-ref="myPointcut" /><!-- 最终通知 --> <aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/><!-- 后置通知 --> <aop:after-throwing method="afterThrows" pointcut-ref="myPointcut"/><!-- 异常通知 --> <aop:around method="around" pointcut-ref="myPointcut"/><!-- 环绕通知 --> </aop:aspect> </aop:config> </beans> </span>四、通知对象的类LogAdvice
<span style="font-size:18px;">package com.seven.spring.n_aop_xml; import org.aspectj.lang.ProceedingJoinPoint; /** * 通知对象 * @author Administrator * */ public class LogAdvice{ //前置通知 public void before(){ System.out.println("==before=="); } //最终通知 public void after(){ System.out.println("==after=="); } //后置通知 public void afterReturning(){ System.out.println("==afterReturning=="); } //异常通知 public void afterThrows(){ System.out.println("==afterThrows=="); } //环绕通知 public void around(ProceedingJoinPoint joinPoint){ try { System.out.println("环绕通知(前)"); joinPoint.proceed();//执行原方法 System.out.println("环绕通知(后)"); } catch (Throwable e) { throw new RuntimeException(e); } } }</span>
五、测试类MainTest
<span style="font-size:18px;">package com.seven.spring.n_aop_xml; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 使用编程方式实现aop * @author Administrator * */ public class MainTest { @Test public void test1(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml",getClass()); //这里获取的是代理对象 UserService userService = (UserService) ac.getBean("userService"); System.out.println(userService.getClass()); //========================= //使用的是代理对象 userService.saveUser(); System.out.println("--------------"); userService.deleteUser(); System.out.println("--------------"); userService.queryUsers(); System.out.println("--------------"); } }</span>六、当各种通知全部配置,然后执行之后,顺序为:
1.不抛异常时:
==before== (前置通知)
环绕通知(前) (环绕通知)
保存一个User (执行原方法)
==after== (最终通知)
==afterReturning== (后置通知)
环绕通知(后) (环绕通知)
2.抛异常时:
==before== (前置通知) 环绕通知(前) (环绕通知) 查询一个User (执行原方法) ==after== (最终通知) ==afterThrows== (异常通知)
原文链接:https://www.f2er.com/xml/295854.html