Spring AOP usage in JoinPoint

Spring JoinPoint usage

JoinPoint objects

JoinPoint encapsulates information SpringAop in the method section, adding section JoinPoint parameter method, can get to the object encapsulates the method JoinPoint information.

Common API

Method name Features
Signature getSignature(); Packaging acquired signature object information, the target object may be acquired method name, Class belongs to the class information and the like
Object[] getArgs(); Gets parameter object passed in the target method
Object getTarget(); Obtain the proxy object
Object getThis(); Get a proxy object

ProceedingJoinPoint objects

ProceedingJoinPoint object is a sub-interface of JoinPoint, the subject method only in the section in @Around,
adds the following two methods.

Object proceed() throws Throwable //执行目标方法 
Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法 

Demo

Class section

@Aspect
@Component
public class aopAspect { /** * 定义一个切入点表达式,用来确定哪些类需要代理 * execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理 */ @Pointcut("execution(* aopdemo.*.*(..))") public void declareJoinPointerExpression() {} /** * 前置方法,在目标方法执行前执行 * @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写 */ @Before("declareJoinPointerExpression()") public void beforeMethod(JoinPoint joinPoint){ System.out.println("目标方法名为:" + joinPoint.getSignature().getName()); System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); //获取传入目标方法的参数 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("第" + (i+1) + "个参数为:" + args[i]); } System.out.println("被代理的对象:" + joinPoint.getTarget()); System.out.println("代理对象自己:" + joinPoint.getThis()); } /** * 环绕方法,可自定义目标方法执行的时机 * @param pjd JoinPoint的子接口,添加了 * Object proceed() throws Throwable 执行目标方法 * Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法 * 两个方法 * @return 此方法需要返回值,返回值视为目标方法的返回值 */ @Around("declareJoinPointerExpression()") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; try { //前置通知 System.out.println("目标方法执行前..."); //执行目标方法 //result = pjd.proeed(); //用新的参数值执行目标方法 result = pjd.proceed(new Object[]{"newSpring","newAop"}); //返回通知 System.out.println("目标方法返回结果后..."); } catch (Throwable e) { //异常通知 System.out.println("执行目标方法异常后..."); throw new RuntimeException(e); } //后置通知 System.out.println("目标方法执行后..."); return result; } } 

The proxy class

/**
 * 被代理对象
 */
@Component
public class TargetClass { /** * 拼接两个字符串 */ public String joint(String str1, String str2) { return str1 + "+" + str2; } } 

Test category

public class TestAop {
    @Test public void testAOP() { //1、创建Spring的IOC的容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml"); //2、从IOC容器中获取bean的实例 TargetClass targetClass = (TargetClass) ctx.getBean("targetClass"); //3、使用bean String result = targetClass.joint("spring","aop"); System.out.println("result:" + result); } } 

Output

目标方法执行前...
目标方法名为:joint
目标方法所属类的简单类名:TargetClass
目标方法所属类的类名:aopdemo.TargetClass
目标方法声明类型:public
第1个参数为:newSpring 第2个参数为:newAop 被代理的对象:aopdemo.TargetClass@4efc180e 代理对象自己:aopdemo.TargetClass@4efc180e (和上面一样是因为toString方法也被代理了) 目标方法返回结果后... 目标方法执行后... result:newSpring+newAop 

statement

This article belongs to the author of the study notes, if the content is wrong, please enlighten me! Thank you

Reference article:
http://blog.csdn.net/ochangwen/article/details/52557724
http://blog.csdn.net/a9529lty/article/details/7031070



Author: zFxiang_
link: https: //www.jianshu.com/p/90881bfc3241
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/feng9exe/p/10949718.html