一、AOP概述
AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充。面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运行过程。
AOP底层就是采用动态代理模式实现的,采用了两种代理:JDK的动态代理与CGLIB的动态代理。
面向切面编程,就是将交叉业务逻辑封装成切面,利用AOP容器的功能将切面织入到主业务逻辑中。所谓交叉业务逻辑是指,通用的、与主业务逻辑无关的代码。如安全检查、事务、日志等。
若不是用AOP,则会出现代码纠缠,即交叉业务逻辑与主业务逻辑混合在一起,这样会使主业务逻辑变的混杂不清。
二、通知Advice
1、通知详解
(1)前置通知MethodBeforeAdvice
定义前置通知,需要实现MethodBeforeAdvice接口。该接口中有一个方法before(),会在目标方法执行之前执行。
前置通知的特点:
1、在目标方法执行之前执行。
2、不改变目标方法的执行流程,前置通知代码不能阻止目标方法执行。
3、不改变目标方法执行的结果。
举例:
创建IService接口:
package com.ietree.spring.basic.aop.beforeadvice;public interface IService { void doFirst(); void doSecond();}
创建接口的实现类:
package com.ietree.spring.basic.aop.beforeadvice;public class SomeServiceImpl implements IService { @Override public void doFirst() { System.out.println("执行doFirst()方法"); } @Override public void doSecond() { System.out.println("执行doFirst()方法"); }}
创建前置通知类MyMethodBeforeAdvice,该类必须实现MethodBeforeAdvice接口:
package com.ietree.spring.basic.aop.beforeadvice;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;/** * 前置通知 * * @author Root */public class MyMethodBeforeAdvice implements MethodBeforeAdvice { // 当前方法在目标方法执行之前执行 // method:目标方法 // args:目标方法参数列表 // target:目标对象 @Override public void before(Method method, Object[] args, Object target) throws Throwable { // 对于目标方法的增强代码就写在这里 System.out.println("执行前置通知..."); }}
配置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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="someService" class="com.ietree.spring.basic.aop.beforeadvice.SomeServiceImpl"/> <bean id="myAdvice" class="com.ietree.spring.basic.aop.beforeadvice.MyMethodBeforeAdvice"/> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"/> <property name="interceptorNames" value="myAdvice"/> </bean> </beans>
测试:
package com.ietree.spring.basic.aop.beforeadvice;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest { @Test public void test() { String resource = "com/ietree/spring/basic/aop/beforeadvice/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); IService service = (IService)ac.getBean("serviceProxy"); service.doFirst(); System.out.println("=============="); service.doSecond(); }}
输出:
执行前置通知...执行doFirst()方法==============执行前置通知...执行doFirst()方法
注意:执行之前需要导入spring-aop-4.3.9.RELEASE.jar包
(2)后置通知AfterReturningAdvice
定义前置通知,需要实现AfterReturningAdvice接口。该接口中有一个方法afterReturning(),会在目标方法执行之后执行。
后置通知的特点:
1、在目标方法执行之后执行。
2、不改变目标方法的执行流程,后置通知代码不能阻止目标方法执行。
3、不改变目标方法执行的结果。
大致流程和前置通知差不多,这里就简单列举一下不同之处:
创建后置通知类并实现AfterReturningAdvice接口,重写afterReturning()方法:
package com.ietree.spring.basic.aop.afterreturningadvice;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;/** * 后置通知:可以获取到目标方法的返回结果,但是无法改变目标方法的结果 * * @author Root */public class MyAfterReturningAdvice implements AfterReturningAdvice { // 在目标方法执行之后执行 // returnValue:目标方法的返回值 @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行后置通知方法 returnValue = " + returnValue); }}
配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="someService" class="com.ietree.spring.basic.aop.afterreturningadvice.SomeServiceImpl"/> <bean id="myAdvice" class="com.ietree.spring.basic.aop.afterreturningadvice.MyAfterReturningAdvice"/> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"/> <property name="interceptorNames" value="myAdvice"/> </bean> </beans>
注意:后置通知可以获取到目标方法的返回结果,但是无法改变目标方法执行的返回结果。
(3)环绕通知MethodInterceptor
定义环绕通知,需要实现MethodInterceptor接口。环绕通知,也叫方法拦截器,可以在目标方法调用之前及之后做处理,可以改变目标方法的返回值,也可以改变程序执行流程。
创建环绕通知类MyMethodInterceptor,实现MethodInterceptor接口:
package com.ietree.spring.basic.aop.methodinterceptor;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;// 环绕通知:可以修改目标方法的返回结果public class MyMethodInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("执行环绕通知:目标方法执行之前"); // 执行目标方法 Object result = invocation.proceed(); System.out.println("执行环绕通知:目标方法执行之后"); if(null != result) { result = ((String)result).toUpperCase(); } return result; }}
配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="someService" class="com.ietree.spring.basic.aop.methodinterceptor.SomeServiceImpl"/> <bean id="myAdvice" class="com.ietree.spring.basic.aop.methodinterceptor.MyMethodInterceptor"/> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"/> <property name="interceptorNames" value="myAdvice"/> </bean> </beans>
测试:
package com.ietree.spring.basic.aop.methodinterceptor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest { @Test public void test() { String resource = "com/ietree/spring/basic/aop/methodinterceptor/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); IService service = (IService)ac.getBean("serviceProxy"); service.doFirst(); System.out.println("=============="); String result = service.doSecond(); System.out.println(result); }}
输出:
执行环绕通知:目标方法执行之前执行doFirst()方法执行环绕通知:目标方法执行之后==============执行环绕通知:目标方法执行之前执行doSecond()方法执行环绕通知:目标方法执行之后ABCDE
注意:环绕通知不仅可以获取到方法的返回结果,而且还可以修改方法的返回结果。
(4)异常通知ThrowsAdvice
异常分两种:1)运行时异常,不进行处理,也可以通过编译。若一个类继承自RunTimeException,则该异常就是运行时异常 2)编译时异常,受查异常,Checked Exception。不进行处理,则无法通过编译。若一个类继承自Exception,则该异常就是受查异常。
创建接口IService:
package com.ietree.spring.basic.aop.throwsadvice;public interface IService { // 用户登录 boolean login(String username, String password) throws UserException;}
创建接口实现类SomeServiceImpl:
package com.ietree.spring.basic.aop.throwsadvice;public class SomeServiceImpl implements IService { @Override public boolean login(String username, String password) throws UserException { if (!"jack".equals(username)) { throw new UsernameException("用户名错误!"); } if (!"123".equals(password)) { throw new PasswordException("密码错误!"); }// double i = 3 / 0; return true; }}
创建三个自定义异常:
package com.ietree.spring.basic.aop.throwsadvice;/** * 自定义异常 * 异常分两种: * 1)运行时异常,不进行处理,也可以通过编译。若一个类继承自RunTimeException,则该异常就是运行时异常 * 2)编译时异常,受查异常,Checked Exception。不进行处理,则无法通过编译。若一个类继承自Exception,则该异常就是受查异常。 * * @author Root */public class UserException extends Exception { public UserException() { super(); } public UserException(String message) { super(message); }}
用户名异常:
package com.ietree.spring.basic.aop.throwsadvice;public class UsernameException extends UserException { public UsernameException() { super(); } public UsernameException(String message) { super(message); }}
密码异常:
package com.ietree.spring.basic.aop.throwsadvice;public class PasswordException extends UserException { public PasswordException() { super(); } public PasswordException(String message) { super(message); }}
定义异常通知:
package com.ietree.spring.basic.aop.throwsadvice;import org.springframework.aop.ThrowsAdvice;/** * 异常通知 当目标方法抛出与指定类型的异常具有is-a关系的异常时,执行当前方法afterThrowing() * * @author Root */public class MyThrowsAdvice implements ThrowsAdvice { // 当目标方法抛出UsernameException异常时,执行当前方法 public void afterThrowing(UsernameException ex) { System.out.println("发生用户名异常 ex = " + ex.getMessage()); } // 当目标方法抛出UsernameException异常时,执行当前方法 public void afterThrowing(PasswordException ex) { System.out.println("发生密码异常 ex = " + ex.getMessage()); } // 当目标方法抛出UsernameException异常时,执行当前方法 public void afterThrowing(Exception ex) { System.out.println("发生其它异常 ex = " + ex.getMessage()); }}
配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="someService" class="com.ietree.spring.basic.aop.throwsadvice.SomeServiceImpl"/> <bean id="myAdvice" class="com.ietree.spring.basic.aop.throwsadvice.MyThrowsAdvice"/> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"/> <property name="interceptorNames" value="myAdvice"/> </bean> </beans>
测试:
package com.ietree.spring.basic.aop.throwsadvice;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest { @Test public void test() throws UserException { String resource = "com/ietree/spring/basic/aop/throwsadvice/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); IService service = (IService)ac.getBean("serviceProxy"); service.login("jack", "123"); }}
(5)同时使用多个通知的配置方法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="someService" class="com.ietree.spring.basic.aop.multipleadvice.SomeServiceImpl"/> <bean id="beforeAdvice" class="com.ietree.spring.basic.aop.multipleadvice.MyMethodBeforeAdvice"/> <bean id="afterAdvice" class="com.ietree.spring.basic.aop.multipleadvice.MyAfterReturningAdvice"/> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"/> <property name="interceptorNames" value="beforeAdvice,afterAdvice"/> </bean> </beans>
三、顾问Advisor
通知(Advice)是Spring提供的一种切面(Aspect)。但其功能过于简单:只能将切面织入到目标类的所有方法中,无法完成将切面织入到指定目标方法中。
顾问(Advisor)是Spring提供的另一种切面。它可以完成更为复杂的切面织入功能。PointcutAdvisor是顾问的一种,可以指定具体的切入点。顾问将通知进行了包装,会根据不同的通知类型,在不同的时间点,将切面织入到不同的切入点。
PointcutAdvisor接口有两个较为常用的实现类:
NameMatchMethodPointcutAdvisor:名称匹配方法切入点顾问
RegexpMethodPointcutAdvisor:正则表达式匹配方法切入点顾问
1、名称匹配方法切入点顾问NameMatchMethodPointcutAdvisor
NameMatchMethodPointcutAdvisor,即名称匹配方法切入点顾问。容器可根据配置文件中指定的方法名来设置切入点。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="someService" class="com.ietree.spring.basic.aop.advisor.namematchmethodpointcutadvisor.SomeServiceImpl"/> <bean id="myAdvice" class="com.ietree.spring.basic.aop.advisor.namematchmethodpointcutadvisor.MyMethodBeforeAdvice"/> <bean id="myAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="myAdvice"/> <property name="mappedNames" value="ir"/> </bean> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"/> <property name="interceptorNames" value="myAdvisor"/> </bean> </beans>
2、正则表达式匹配方法切入点顾问RegexpMethodPointcutAdvisor
RegexpMethodPointcutAdvisor,即正则表达式方法顾问。容器可根据正则表达式来设置切入点。注意,与正则表达式进行匹配的对象是接口中的方法名,而非目标类(接口实现类)的方法名。
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="someServiceImpl" class="com.ietree.spring.aop.advisor.SomeServiceImpl"></bean> <bean id="myAdvice" class="com.ietree.spring.aop.advisor.MyMethodBeforeAdvice"></bean> <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="myAdvice"></property> </bean> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someServiceImpl"/> <property name="interceptorNames" value="myAdvisor"/> </bean></beans>
测试:
package com.ietree.spring.aop.advisor;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by Root on 2017/7/3. */public class Test { @org.junit.Test public void test01(){ String resource = "com/ietree/spring/aop/advisor/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); ISomeService service = (ISomeService)ac.getBean("serviceProxy"); service.doFirst(); System.out.println("=========================="); service.doSecond(); System.out.println("=========================="); service.doThird(); }}
四、自动代理生成器
1、默认advisor自动代理生成器
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <bean id="someServiceImpl" class="com.ietree.spring.aop.defaultadvisorautoproxycreator.SomeServiceImpl"></bean> <bean id="someServiceImpl2" class="com.ietree.spring.aop.defaultadvisorautoproxycreator.SomeServiceImpl"></bean> <bean id="myAdvice" class="com.ietree.spring.aop.defaultadvisorautoproxycreator.MyMethodBeforeAdvice"></bean> <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="myAdvice"></property> <property name="pattern" value=".*doFirst"/> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/></beans>
测试:
package com.ietree.spring.aop.defaultadvisorautoproxycreator;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by Root on 2017/7/3. */public class Test { @org.junit.Test public void test01(){ String resource = "com/ietree/spring/aop/defaultadvisorautoproxycreator/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); ISomeService service = (ISomeService)ac.getBean("someServiceImpl"); service.doFirst(); System.out.println("=========================="); service.doSecond(); System.out.println("=========================="); service.doThird(); System.out.println("----------------------------------"); ISomeService service2 = (ISomeService)ac.getBean("someServiceImpl2"); service2.doFirst(); System.out.println("=========================="); service2.doSecond(); System.out.println("=========================="); service2.doThird(); }}
注意:使用DefaultAdvisorAutoProxyCreator自动代理生成器会有以下几个问题:
1)不能选择目标对象。
2)不能选择切面类型,切面只能是advisor。
3)不能选择advisor,所有的advisor都将被作为切面织入到目标方法中。
需要解决这几个问题,需要使用Bean名称自动代理生成器实现。
2、Bean名称自动代理生成器
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <bean id="someService" class="com.ietree.spring.aop.defaultadvisorautoproxycreator.SomeServiceImpl"></bean> <bean id="someService2" class="com.ietree.spring.aop.defaultadvisorautoproxycreator.SomeServiceImpl"></bean> <bean id="myAdvice" class="com.ietree.spring.aop.defaultadvisorautoproxycreator.MyMethodBeforeAdvice"></bean> <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="myAdvice"></property> <property name="pattern" value=".*doFirst"/> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="someService"/> <property name="interceptorNames" value="myAdvisor"/> </bean></beans>
五、AspectJ对AOP的实现
1、AspectJ基本概念
AspectJ切入表达式原型:
execution( [modifiers-pattern] 访问权限类型 ret-type-pattern 返回值类型 [declaring-type-pattern] 全限定性类名 name-pattern(param-pattern)方法名(参数名) [throws-pattern] 抛出异常类型 )
其中,带有[ ]的部分是可以省略的,红色的部分是不能省略的部分。
还可以使用以下符号:
符号
意义
0至多个任意字符
..
用在方法参数中,表示任意多个参数。
用在包名后,表示当前包及其子包路径。
用在类名后,表示当前类及其子类。
用在接口后,表示当前接口及其实现类。
2、基于注解方式的实现
首先创建切面类 MyAspectJ:
package com.ietree.spring.aop.aspectj.annotation;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.;import org.springframework.util.StringUtils;import java.util.Locale;/* * 切面类 * <p> * Created by Root on 2017/7/5. /@Aspect // 表示当前类为切面类public class MyAspectJ { // 设置前置通知 @Before("execution( ..ISomeService.doFirst(..))") public void before() { System.out.println("执行前置通知方法..."); } @Before("execution( ..ISomeService.doFirst(..))") public void before(JoinPoint jp) { System.out.println("执行前置通知方法,jp = " + jp); } // 设置后置通知 @AfterReturning("execution( ..ISomeService.doSecond(..))") public void afterReturning() { System.out.println("执行后置通知方法..."); } @AfterReturning(value = "execution( ..ISomeService.doSecond(..))", returning = "result") public void afterReturning(Object result) { System.out.println("执行后置通知方法, result = " + result); } // 设置环绕通知 @Around("execution( ..ISomeService.doSecond(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("执行环绕通知方法之前..."); // 执行目标方法 Object result = pjp.proceed(); System.out.println("执行环绕通知方法之后..."); if (!StringUtils.isEmpty(result)) { result = ((String) result).toUpperCase(Locale.US); } return result; } // 设置异常通知 @AfterThrowing("execution( ..ISomeService.doThird(..))") public void afterThrowing() { System.out.println("执行异常通知方法..."); } // 设置异常通知 @AfterThrowing(value = "execution( ..ISomeService.doThird(..))", throwing = "ex") public void afterThrowing(Exception ex) { System.out.println("执行异常通知方法,ex = " + ex.getMessage()); } // 设置最终通知 //@After("doThirdPointCut()") @After("execution( ..ISomeService.doThird(..))") public void After() { System.out.println("执行最终通知方法"); } // 定义一个切入点,叫做doThirdPointCut(),可以用来替换掉同样的切入点表达式,例如替换后的最终通知 @Pointcut("execution( *..ISomeService.doThird(..))") public void doThirdPointCut() { }}
配置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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="myService" class="com.ietree.spring.aop.aspectj.annotation.SomeServiceImpl"/> <bean id="myAspectJ" class="com.ietree.spring.aop.aspectj.annotation.MyAspectJ"/> <aop:aspectj-autoproxy/></beans>
3、基于XML方式的实现
首先创建切面类 MyAspectJ:
package com.ietree.spring.aop.aspectj.xml;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.;import org.springframework.util.StringUtils;import java.util.Locale;/* * 切面类 * <p> * Created by Root on 2017/7/5. */public class MyAspectJ { // 设置前置通知 public void before() { System.out.println("执行前置通知方法..."); } public void before(JoinPoint jp) { System.out.println("执行前置通知方法,jp = " + jp); } // 设置后置通知 public void afterReturning() { System.out.println("执行后置通知方法..."); } public void afterReturning(Object result) { System.out.println("执行后置通知方法, result = " + result); } // 设置环绕通知 public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("执行环绕通知方法之前..."); // 执行目标方法 Object result = pjp.proceed(); System.out.println("执行环绕通知方法之后..."); if (!StringUtils.isEmpty(result)) { result = ((String) result).toUpperCase(Locale.US); } return result; } // 设置异常通知 public void afterThrowing() { System.out.println("执行异常通知方法..."); } // 设置异常通知 public void afterThrowing(Exception ex) { System.out.println("执行异常通知方法,ex = " + ex.getMessage()); } // 设置最终通知 public void after() { System.out.println("执行最终通知方法"); }}
配置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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="myService" class="com.ietree.spring.aop.aspectj.xml.SomeServiceImpl"/> <bean id="myAspectJ" class="com.ietree.spring.aop.aspectj.xml.MyAspectJ"/> <aop:config> <aop:pointcut id="doFirstPointcut" expression="execution(* ..ISomeService.doFirst(..))"/> <aop:pointcut id="doSecondPointcut" expression="execution( ..ISomeService.doSecond(..))"/> <aop:pointcut id="doThirdPointcut" expression="execution( *..ISomeService.doThird(..))"/> <aop:aspect ref="myAspectJ"> <aop:before method="before" pointcut-ref="doFirstPointcut"/> <aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirstPointcut"/> <aop:after-returning method="afterReturning" pointcut-ref="doSecondPointcut"/> <aop:after-returning method="afterReturning(java.lang.Object)" pointcut-ref="doSecondPointcut" returning="result"/> <aop:around method="around" pointcut-ref="doSecondPointcut"/> <aop:after-throwing method="afterThrowing(java.lang.Exception)" pointcut-ref="doThirdPointcut" throwing="ex"/> <aop:after method="after" pointcut-ref="doThirdPointcut"/> </aop:aspect> </aop:config></beans>
程序输出:
执行前置通知方法...执行前置通知方法,jp = execution(void com.ietree.spring.aop.aspectj.xml.ISomeService.doFirst())执行doFirst()方法-----------------执行环绕通知方法之前...执行doSecond()方法执行环绕通知方法之后...执行后置通知方法, result = ABCDE执行后置通知方法...-----------------执行最终通知方法执行异常通知方法,ex = / by zerojava.lang.ArithmeticException: / by zero