AOP implements intercepting objects as well as getting into target methods and annotations __java

Source: Internet
Author: User
Tags aop throwable
AOP implements intercepting objects and obtaining approach and annotations to cut targets

One,joinpoint is what.

ASPECTJ uses the Org.aspectj.lang.JoinPoint interface to represent the target class connection point object, and if it is a surround enhancement, use the Org.aspectj.lang.ProceedingJoinPoint represents a connection point object, which is a joinpoint sub-interface. Any enhancement method can access information to the connection point context by declaring the first argument as Joinpoint.

Second, we first look at the main methods of these two interfaces :

1.JoinPoint
L java.lang.object[] Getargs (): Gets the parameter list of the connection point method runtime;
L Signature getsignature (): Gets the method signature object of the connection point;
L Java.lang.Object gettarget (): Gets the target object where the connection point is located;
L Java.lang.Object getthis (): Gets the proxy object itself;

2.ProceedingJoinPoint

Proceedingjoinpoint inherits the Joinpoint Sub-interface, which adds two methods for performing the connection point method:

Java.lang.Object proceed () throws Java.lang.Throwable: A method for performing the connection point of the target object through reflection;

Java.lang.Object Proceed (java.lang.object[] args) throws Java.lang.Throwable:

Executes the method at the target object connection point by reflection, but replaces the original entry with the new argument.

Third, to learn more about the Joinpoint interface and implementation classes:

Three ways to get into the method object:

1. Method of obtaining the target class of pointcut through reflection mechanism

public void Invoke (Joinpoint joinpoint) throws throwable{

//logon interception

Methodinvocationproceedingjoinpoint Methodpoint = (methodinvocationproceedingjoinpoint) joinpoint;

Field proxy = Methodpoint.getclass (). Getdeclaredfield ("Methodinvocation");

Proxy.setaccessible (true);

Reflectivemethodinvocation j = (reflectivemethodinvocation) proxy.get (methodpoint);

Method method = J.getmethod ();

Login login = method.getannotation (login.class);

}


2. Get the target object of the connection point through the Joinpoint gettarget ()

public void Invoke (Joinpoint joinpoint) throws throwable{

//Interceptor entity class

Object target = Joinpoint.gettarget ();

Blocked method name

String methodname = Joinpoint.getsignature (). GetName ();

Intercept method Parameters

object[] Argsa = Joinpoint.getargs ();

Intercepting parameter type

class[] parametertypes = ((methodsignature) joinpoint.getsignature ()). GetMethod (). getparametertypes ();

Method method = Target.getclass (). GetMethod (methodname, parametertypes);

Login login = method.getannotation (login.class);

}


3. The method signature object that obtains the connection point through the Joinpoint getsignature ()

public void Invoke (Joinpoint joinpoint) throws throwable{

methodsignature methodsignature = (methodsignature) Joinpoint.getsignature ();

Method method = Methodsignature.getmethod ();

Login login = method.getannotation (login.class);

}


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.