目录
Demo
public interface ArithmeticCalculator {
int add(int i, int j);
int div(int i, int j);
}
@Component("arithmeticCalculator")
public class ArithmeticCalculatorIml implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
System.out.println("add->result:" + result);
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
System.out.println("div->result:" + result);
return result;
}
}
1. @Before: 前置通知, 在方法执行之前执行
@Aspect
@Component
public class LogProxy {
@Before("execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))")
public void beforMethod(JoinPoint point){
String methodName = point.getSignature().getName();
List<Object> args = Arrays.asList(point.getArgs());
System.out.println("调用前连接点方法为:" + methodName + ",参数为:" + args);
}
}
调用前连接点方法为:add,参数为:[3, 2]
add->result:5
调用前连接点方法为:div,参数为:[4, 2]
div->result:2
2. @After: 后置通知, 在方法执行之后执行 (发生异常也可以执行)
@Aspect
@Component
public class LogProxy {
@After(("execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))"))
public void afterMethod(JoinPoint point){
String methodName = point.getSignature().getName();
List<Object> args = Arrays.asList(point.getArgs());
System.out.println("调用后连接点方法为:" + methodName + ",参数为:" + args);
}
}
add->result:5
调用后连接点方法为:add,参数为:[3, 2]
div->result:2
调用后连接点方法为:div,参数为:[4, 2]
发现add和div两个连接点方法执行之后都调用了后置方法。如果目标连接点方法出现异常时,也会执行后置通知方法。把测试方法改成如下:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-aop.xml");
ArithmeticCalculator arithmetic = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
arithmetic.add(3, 2);
/*被除数为0,会抛出异常*/
arithmetic.div(4, 0);
}
add->result:5
调用后连接点方法为:add,参数为:[3, 2]
调用后连接点方法为:div,参数为:[4, 0]
Exception in thread "main" java.lang.ArithmeticException: / by zero
at lzj.com.spring.aop.ArithmeticCalculatorIml.div(ArithmeticCalculatorIml.java:17)
……
从输出结果中可以看出,即使目标方法出现异常,后置通知方法依然执行。但后置通知拿不到目标方法执行后的结果,因为目标方法有可能出现异常。如果要拿目标方法的执行结果,要用下面的通知方法。
3. @AfterRunning: 返回通知, 在方法执行成功返回结果之后执行
当连接点方法成功执行后,返回通知方法才会执行,如果连接点方法出现异常,则返回通知方法不执行。返回通知方法在目标方法执行成功后才会执行,所以,返回通知方法可以拿到目标方法(连接点方法)执行后的结果。切面类中定义返回通知方法,示例如下:
@Aspect
@Component
public class LogProxy {
/*通过returning属性指定连接点方法返回的结果放置在result变量中,在返回通知方法中可以从result变量中获取连接点方法的返回结果了。*/
@AfterReturning(value="execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))",
returning="result")
public void afterReturning(JoinPoint point, Object result){
String methodName = point.getSignature().getName();
List<Object> args = Arrays.asList(point.getArgs());
System.out.println("连接点方法为:" + methodName + ",参数为:" + args + ",目标方法执行结果为:" + result);
}
}
add->result:5
连接点方法为:add,参数为:[3, 2],目标方法执行结果为:5
div->result:2
连接点方法为:div,参数为:[4, 2],目标方法执行结果为:2
当连接点方法出现异常时,不执行返回通知方法,把测试方法该为如下:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-aop.xml");
ArithmeticCalculator arithmetic = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
arithmetic.add(3, 2);
arithmetic.div(4, 0);
}
}
add->result:5
连接点方法为:add,参数为:[3, 2],目标方法执行结果为:5
Exception in thread "main" java.lang.ArithmeticException: / by zero
……
从输出结果可以看出,div(4,0)出现异常,因此该连接点对应的返回通知方法也不执行。
4. @AfterThrowing: 异常通知, 在方法抛出异常之后
异常通知方法只在连接点方法出现异常后才会执行,否则不执行。在异常通知方法中可以获取连接点方法出现的异常。在切面类中异常通知方法,示例如下:
/*通过throwing属性指定连接点方法出现异常信息存储在ex变量中,在异常通知方法中就可以从ex变量中获取异常信息了*/
@AfterThrowing(value="execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))",
throwing="ex")
public void afterReturning(JoinPoint point, Exception ex){
String methodName = point.getSignature().getName();
List<Object> args = Arrays.asList(point.getArgs());
System.out.println("连接点方法为:" + methodName + ",参数为:" + args + ",异常为:" + ex);
}
测试方法
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-aop.xml");
ArithmeticCalculator arithmetic = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
arithmetic.add(3, 2);
arithmetic.div(4, 0);
}
}
add->result:5
连接点方法为:div,参数为:[4, 0],异常为:java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
从输出结果中可以看出,add方法没有异常,因此不执行异常通知方法,div方法出现异常,执行科异常通知方法。
上面的例子中,异常类型设置的是Exception,表示捕获连接点方法的所有异常信息,也可以指定捕获指定类型的信息,例如
@AfterThrowing(value="execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))",
throwing="ex")
/*只捕获连接点方法中的NullPointerException 类型的异常信息*/
public void afterReturning(JoinPoint point, NullPointerException ex){
String methodName = point.getSignature().getName();
List<Object> args = Arrays.asList(point.getArgs());
System.out.println("连接点方法为:" + methodName + ",参数为:" + args + ",异常为:" + ex);
}
5. @Around: 环绕通知, 围绕着方法执行
作用
- 既可以在目标方法之前织入增强动作,也可以在执行目标方法之后织入增强动作;
- 可以决定目标方法在什么时候执行,如何执行,甚至可以完全阻止目标目标方法的执行;
- 可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回值; 当需要改变目标方法的返回值时,只能使用Around方法;
Demo
环绕通知方法可以包含上面四种通知方法,环绕通知的功能最全面。环绕通知需要携带 ProceedingJoinPoint 类型的参数,且环绕通知必须有返回值, 返回值即为目标方法的返回值。在切面类中创建环绕通知方法,示例如下:
@Around("execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))")
public Object aroundMethod(ProceedingJoinPoint pdj){
/*result为连接点的放回结果*/
Object result = null;
String methodName = pdj.getSignature().getName();
/*前置通知方法*/
System.out.println("前置通知方法>目标方法名:" + methodName + ",参数为:" + Arrays.asList(pdj.getArgs()));
/*执行目标方法*/
try {
result = pdj.proceed();
/*返回通知方法*/
System.out.println("返回通知方法>目标方法名" + methodName + ",返回结果为:" + result);
} catch (Throwable e) {
/*异常通知方法*/
System.out.println("异常通知方法>目标方法名" + methodName + ",异常为:" + e);
}
/*后置通知*/
System.out.println("后置通知方法>目标方法名" + methodName);
return result;
}
}
测试方法
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-aop.xml");
ArithmeticCalculator arithmetic = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
arithmetic.add(3, 2);
arithmetic.div(4, 0);
}
}
前置通知方法>目标方法名:add,参数为:[3, 2]
add->result:5
返回通知方法>目标方法名add,返回结果为:5
后置通知方法>目标方法名add
前置通知方法>目标方法名:div,参数为:[4, 0]
异常通知方法>目标方法名div,异常为:java.lang.ArithmeticException: / by zero
后置通知方法>目标方法名div
Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int lzj.com.spring.aop.ArithmeticCalculator.div(int,int)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:219)
at com.sun.proxy.$Proxy7.div(Unknown Source)
at lzj.com.spring.aop.Main.main(Main.java:12)
当div目标方法出现异常时,在环绕通知方法中已经用try…catch方法进行捕捉了,为什么最后输出结果中还出现了一个返回类型不匹配的错误:
Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int lzj.com.spring.aop.ArithmeticCalculator.div(int,int)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:219)
at com.sun.proxy.$Proxy7.div(Unknown Source)
at lzj.com.spring.aop.Main.main(Main.java:12)
那是因为在环绕通知方法中开始就定义了目标方法的返回结果
Object result = null。当目标方法出现异常时,result = pdj.proceed();执行时出现异常,此时result中还是null,所以在环绕通知方法最后return result;时,返回的result就是null,但是环绕通知的返回类型我们定义的是Object类型的,null不能转化为Object类型,所以抛出了个类型转换的错误。我们可以在环绕通知方法中把异常抛出去,即为:
@Around("execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))")
public Object aroundMethod(ProceedingJoinPoint pdj){
/*result为连接点的放回结果*/
Object result = null;
String methodName = pdj.getSignature().getName();
/*前置通知方法*/
System.out.println("前置通知方法>目标方法名:" + methodName + ",参数为:" + Arrays.asList(pdj.getArgs()));
/*执行目标方法*/
try {
result = pdj.proceed();
/*返回通知方法*/
System.out.println("返回通知方法>目标方法名" + methodName + ",返回结果为:" + result);
} catch (Throwable e) {
/*异常通知方法*/
System.out.println("异常通知方法>目标方法名" + methodName + ",异常为:" + e);
/*当环绕通知方法本身还有其它异常时,非连接点方法出现的异常,此时抛出来*/
throw new RuntimeException();
}
/*后置通知*/
System.out.println("后置通知方法>目标方法名" + methodName);
return result;
}
}
来源:https://blog.csdn.net/u010502101/article/details/78823056