Instructions for using JoinPoint object based on SpringAop

  • 2021-09-20 20:18:49
  • OfStack

JoinPoint object

The JoinPoint object encapsulates the information of the section method in SpringAop, and the JoinPoint object encapsulating the information of the method can be obtained by adding JoinPoint parameter to the section method.

Commonly used api:

方法名 功能
Signature getSignature(); 获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
Object[] getArgs(); 获取传入目标方法的参数对象
Object getTarget(); 获取被代理的对象
Object getThis(); 获取代理对象

ProceedingJoinPoint Object

The ProceedingJoinPoint object is a subinterface of JoinPoint and is used only in the facet method of @ Around.

Added


Object proceed() throws Throwable // Execute the target method 
Object proceed(Object[] var1) throws Throwable // The new parameter passed in to execute the target method 

Two methods.

Demo

Section class


@Aspect
@Component
public class aopAspect {
    /**
     *  Definition 1 Entry point expressions , Used to determine which classes require proxies 
     * execution(* aopdemo.*.*(..)) Representative aopdemo All methods of all classes under the package will be proxy 
     */
    @Pointcut("execution(* aopdemo.*.*(..))")
    public void declareJoinPointerExpression() {}
    /**
     *  Prepositional method , Execute before the target method executes 
     * @param joinPoint  An object encapsulating proxy method information , If you don't use it, you can ignore it 
     */
    @Before("declareJoinPointerExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println(" The target method is named :" + joinPoint.getSignature().getName());
        System.out.println(" Simple class name of the class to which the target method belongs :" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println(" Class name of the class to which the target method belongs :" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println(" Target method declaration type :" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        // Gets the parameters passed into the target method 
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println(" No. 1 " + (i+1) + " Parameters are :" + args[i]);
        }
        System.out.println(" Proxy object :" + joinPoint.getTarget());
        System.out.println(" Proxy object itself :" + joinPoint.getThis());
    }
    /**
     *  Surrounding method , You can customize when the target method executes 
     * @param pjd JoinPoint Sub-interface of , Added 
     *            Object proceed() throws Throwable  Execute the target method 
     *            Object proceed(Object[] var1) throws Throwable  The new parameter passed in to execute the target method 
     *             Two methods 
     * @return  This method requires a return value , The return value is treated as the return value of the target method 
     */
    @Around("declareJoinPointerExpression()")
    public Object aroundMethod(ProceedingJoinPoint pjd){
        Object result = null;
        try {
            // Pre-notification 
            System.out.println(" Before the target method executes ...");
            // Execute the target method 
            //result = pjd.proeed();
            // Execute the target method with the new parameter value 
            result = pjd.proceed(new Object[]{"newSpring","newAop"});
            // Return notification 
            System.out.println(" After the target method returns the result, ...");
        } catch (Throwable e) {
            // Exception notification 
            System.out.println(" After executing the target method exception, ...");
            throw new RuntimeException(e);
        }
        // Post notification 
        System.out.println(" After the target method executes ...");
        return result;
    }
}

Proxy class


/**
 *  Proxy object 
 */
@Component
public class TargetClass {
    /**
     *  Splice two strings 
     */
    public String joint(String str1, String str2) {
        return str1 + "+" + str2;
    }
}

Test class


public class TestAop {
    @Test
    public void testAOP() {
        //1 , creating Spring Adj. IOC Container of 
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");
        //2 , from IOC Obtain in the container bean Example of 
        TargetClass targetClass = (TargetClass) ctx.getBean("targetClass");
        //3 , using bean
        String result = targetClass.joint("spring","aop");
        System.out.println("result:" + result);
    }
}

Output result

Before the target method executes...
Target method name: joint
Simple class name of the class to which the target method belongs: TargetClass
Class name of the class to which the target method belongs: aopdemo. TargetClass
Target method declaration type: public
The first parameter is: newSpring
The second parameter is: newAop
Proxy object: aopdemo. TargetClass @ 4efc180e
The proxy object itself: aopdemo. TargetClass @ 4efc180e
After the target method returns the result...
After the target method executes...
result:newSpring+newAop

Several commonly used API of SpringAop notification parameters JoinPoint

1. Get the class name


String className = joinPoint.getSignature().getDeclaringTypeName();

2. Get the method name


String methodName = joinPoint.getSignature().getName();

3. Get the return value type


MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
Class clazz = methodSignature.getReturnType();

If it is around the notification parameter ProceedingJoinPoint, there is

4. Implement the target method


joinPoint.proceed();

5. Execute the target method and replace the input parameter


proceed(java.lang.Object[] args)

Related articles: