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