Parameter Operation of Obtaining Entry Point Method with joinpoint in springAOP

  • 2021-09-20 20:27:15
  • OfStack

springAOP joinpoint Get the parameters of the pointcut method

Concepts:

Access the target method with jointpoint (@ around with poceedingjointpoint)

1. Get their target object information, such as test. component @ 80387a

2. There are also parameters for methods with parameters, such as [Ljava. lang. Object; @ 183cfe9 (take it for granted that we can also restore the object with its arrays. toString () method)

3. There is also information about obtaining enhanced methods such as String test. component. test1 (String)

Summary: There are 3 ways to access the target method parameters (actually there are 4, let's talk about 3 first)

joinpoint. getargs (): Get the parameters of the parameterized method

Note: It is to get the parameters in the test method in the component. If there are multiple parameters in the test method, then this method will return multiple parameters. Which one you want is filtered by for loop plus judgment

2. joinpoint. getTarget (): Get their target object information

3. joinpoint. getSignature (): (signature means signal, identification): Gets information about the enhanced method. There are two subsequent methods

getDeclaringTypeName: Returns the package name and class name of the method

getname (): Returns method name

Don't understand, please look at the code!

Component class


@Component
public class component {
    public void test() {
    }
    public void test1(String name2) {
    }
}

I take @ afterreturning as an example to explain

Let's look at the 3 Signature method first


@AfterReturning(value="execution(* test.*.*(..))",returning="name2")
    private void test1(JoinPoint jp ,String name2) {
                       System.out.println(jp.getSignature());
                       System.out.println(jp.getSignature().getName());
                System.out.println(jp.getSignature().getDeclaringTypeName());
}

Output Results of Signature Method

You see,

getSignature ()); Is to get such information as modifier + package name + component name (class name) + method name

getSignature (). getName ()); : Method name

getSignature (). getDeclaringTypeName ()): Package name + component name (class name)

Let's look at 1 getargs () again


System.out.println(jp.getArgs());
System.out.println(Arrays.toString(jp.getArgs()));

Result output

jp. getArgs (): We get an object that doesn't understand a set of arrays, but we know it's an object, the parameter object Arrays. toString (jp. getArgs ()), but we can use the array-to-string method in the Arrays class: arrays. tostring (returning the specified array content as a string) to get the exact number

3. The getTarget () method: This method returns the target object that was woven into the enhancement process. (This is similar to the getthis () method, but the value is not equal.)


System.out.println(jp.getTarget());

Output result

Is a real object, which is neither a parameter nor related information

Summary: These are all methods that access the parameters of the target method (except that getthis () does not say that it is actually a proxy object generated by returning 1 object

The resulting object is very similar to the gettarget () method, almost identical, but I've used. eqauls and = = are not equal.) These methods remind me of the document (dom) programmed fetch element docuemnt. getelementbyid (); Get these elements to modify or observe whether your code is different.

Use JoinPoint to get the notified method parameters and pass them to the notification method

Provides access to the target object, proxy object, method parameters and other data of the currently notified method


package org.aspectj.lang;
import org.aspectj.lang.reflect.SourceLocation;
public interface JoinPoint {
    String toString();         // Information about the location of the connection point 
    String toShortString();     // Short information about the location of the connection point 
    String toLongString();     // All relevant information about the location of the connection point 
    Object getThis();         // Return AOP Proxy object 
    Object getTarget();       // Return the target object 
    Object[] getArgs();       // Returns a list of notified method parameters 
    Signature getSignature();  // Returns the signature of the current connection point 
    SourceLocation getSourceLocation();// Returns the location of the join point method in the class file 
    String getKind();        // Connection point type 
    StaticPart getStaticPart(); // Returns the static part of the connection point 
}

Used to surround notifications, using the proceed () method to execute the target method


public interface ProceedingJoinPoint extends JoinPoint {
    public Object proceed() throws Throwable;
    public Object proceed(Object[] args) throws Throwable;
}

Access static parts of join points, such as notified method signatures, join point types, and so on


public interface StaticPart {
Signature getSignature();    // Returns the signature of the current connection point 
String getKind();          // Connection point type 
    int getId();               // Only 1 Identification 
String toString();         // Information about the location of the connection point 
    String toShortString();     // Short information about the location of the connection point 
    String toLongString();     // All relevant information about the location of the connection point 
}

Special note: JoinPoint must be the first parameter


Related articles: