本文主要是介绍Spring中的AOP @Around @Before @AfterReturning @After,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
@Around是可以同时在所拦截方法的前后执行一段逻辑。
@Before是在所拦截方法执行之前执行一段逻辑。
@After 是在所拦截方法执行之后执行一段逻辑。
注意:不能拦截静态(static)方法
示例:
package com.abc.advice;import java.util.Arrays;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;@Aspect
public class AdviceTest {@Around("execution(* com.abc.service.*.many*(..))")public Object process(ProceedingJoinPoint point) throws Throwable {System.out.println("@Around:执行目标方法之前...");//访问目标方法的参数:Object[] args = point.getArgs();if (args != null && args.length > 0 && args[0].getClass() == String.class) {args[0] = "改变后的参数1";}//用改变后的参数执行目标方法Object returnValue = point.proceed(args);System.out.println("@Around:执行目标方法之后...");System.out.println("@Around:被织入的目标对象为:" + point.getTarget());return "原返回值:" + returnValue + ",这是返回结果的后缀";}@Before("execution(* com.abc.service.*.many*(..))")public void permissionCheck(JoinPoint point) {System.out.println("@Before:模拟权限检查...");System.out.println("@Before:目标方法为:" + point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName());System.out.println("@Before:参数为:" + Arrays.toString(point.getArgs()));System.out.println("@Before:被织入的目标对象为:" + point.getTarget());}@AfterReturning(pointcut="execution(* com.abc.service.*.many*(..))", returning="returnValue")public void log(JoinPoint point, Object returnValue) {System.out.println("@AfterReturning:模拟日志记录功能...");System.out.println("@AfterReturning:目标方法为:" + point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName());System.out.println("@AfterReturning:参数为:" + Arrays.toString(point.getArgs()));System.out.println("@AfterReturning:返回值为:" + returnValue);System.out.println("@AfterReturning:被织入的目标对象为:" + point.getTarget());}@After("execution(* com.abc.service.*.many*(..))")public void releaseResource(JoinPoint point) {System.out.println("@After:模拟释放资源...");System.out.println("@After:目标方法为:" + point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName());System.out.println("@After:参数为:" + Arrays.toString(point.getArgs()));System.out.println("@After:被织入的目标对象为:" + point.getTarget());}
}
被拦截的方法
//将被AdviceTest的各种方法匹配
public String manyAdvices(String param1, String param2) {System.out.println("方法:manyAdvices");return param1 + " 、" + param2;
}
运行方法
String result = manager.manyAdvices("aa", "bb");
System.out.println("Test方法中调用切点方法的返回值:" + result);
结果
@Around:执行目标方法之前...
@Before:模拟权限检查...
@Before:目标方法为:com.abc.service.AdviceManager.manyAdvices
@Before:参数为:[改变后的参数1, bb]
@Before:被织入的目标对象为:com.abc.service.AdviceManager@1dfc617e
方法:manyAdvices
@Around:执行目标方法之后...
@Around:被织入的目标对象为:com.abc.service.AdviceManager@1dfc617e
@After:模拟释放资源...
@After:目标方法为:com.abc.service.AdviceManager.manyAdvices
@After:参数为:[改变后的参数1, bb]
@After:被织入的目标对象为:com.abc.service.AdviceManager@1dfc617e
@AfterReturning:模拟日志记录功能...
@AfterReturning:目标方法为:com.abc.service.AdviceManager.manyAdvices
@AfterReturning:参数为:[改变后的参数1, bb]
@AfterReturning:返回值为:原返回值:改变后的参数1 、 bb,这是返回结果的后缀
@AfterReturning:被织入的目标对象为:com.abc.service.AdviceManager@1dfc617e
Test方法中调用切点方法的返回值:原返回值:改变后的参数1 、bb,这是返回结果的后缀
原文地址:https://my.oschina.net/itblog/blog/211693
这篇关于Spring中的AOP @Around @Before @AfterReturning @After的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!