Use Joinpoint to access parameters of the target method in spring

Source: Internet
Author: User
Tags arrays

Concept: The access target method is used Jointpoint (@around with Poceedingjointpoint) 1. Get their target object information, such as test.component@80387a, 2. There are also parameters to get the parameter method, such as [ Ljava.lang.object;@183cfe9 (Take it for granted that we can also use the object's Arrays.tostring () method to restore it) 3. There is also information about how to obtain enhanced methods such as String Test.component.test1 ( String) This

Summary: Access to the target method parameters, there are three methods (there are four, the first to say three kinds)

1.joinpoint.getargs (): Gets the parameters of the parameter method

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

3..joinpoint.getsignature ():(signature is the signal, meaning of the identification): Obtain information about the enhanced method

If you don't understand, look at the code!

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 ());

In fact, the Getsignature () method also has useful methods, such as: Getdeclaringtypename and GetName (); The former is the package name and class name of the return method, and the latter is the return method name

System.out.println (Jp.getsignature (). GetName ());
System.out.println (Jp.getsignature (). Getdeclaringtypename ());

}

Component classes
@Component
public class Component {

public void Test () {

}
public void Test1 (String name2) {

}
}

Output results under Signature method


You see, getsignature ()); Is get to such information modifier + package name + Component Name (class name) + method name

Getsignature (). GetName ()); Method name

Getsignature (). Getdeclaringtypename ()); Package name + Component Name (class name)

We'll see 1 Getargs ()

System.out.println (Jp.getargs ());
System.out.println (Arrays.tostring (Jp.getargs ()));


Result output



Jp.getargs () We'll get a set of array objects that we don't understand, but we know that this is an object, a Parameter object
Arrays.tostring (Jp.getargs ()) but we can use the Arrays class to go to the string method: Arrays.tostring (return the specified array of contents as a string representation) to get a specific number


3.getTarget () Method: The method returns the target object that is being woven into the enhanced processing. (This is similar to the Getthis () method But the values are not equal)

System.out.println (Jp.gettarget ());

Output results



is a real object, neither a parameter nor a relevant information


Summarize:

The above is all methods to access the parameters of the target method (except Getthis () does not actually return an object generated by the proxy object derived from the object is similar to the Gettarget () method is almost identical, but I used. eqauls and = = are not equal) these methods,  Reminds me of the Acquisition element Docuemnt.getelementbyid () of document (DOM) programming; Get these elements to modify or to see if your code is strange.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.