Spring principles-classic AOP
Aop is a plane-oriented programming. My understanding is to package it and set it layer by layer.
First, let's take a look at how to return the proxy of an object.
Package agint; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy;/*** print the log * @ author Administrator **/public class ShowLogHanlder implements InvocationHandler {/*** target Object */private Object target; /*** Proxy initialization * @ param target * @ return */public static Object initNextAngient (Object target) {return Proxy. newProxyInstance (target. getClass (). getClassLoader (), target. getClass (). getInterfaces (), new ShowLogHanlder (target);} public ShowLogHanlder (Object target) returns super({this.tar get = target ;} /*** implement your own proxy logic */@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System. out. println ("computing start"); Object obj = method. invoke (target, args); System. out. println ("computing end"); return obj ;}}
The purpose of this class is to call the static initNextAngient to return the proxy of the target object.
How can we implement nesting of multiple proxy objects?
Package agint; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; public class ChackValueHanlder implements InvocationHandler {private Object target; public static Object initNextAngient (Object target) {return Proxy. newProxyInstance (target. getClass (). getClassLoader (), target. getClass (). getInterfaces (), new ChackValueHanlder (target);} public ChackValueHanlder (Object target?#this.tar get = target;} public void CheakValue (Object obj) {double value = (Double) obj; if (value <0) {System. out. println ("data must be greater than 0");} else {System. out. println ("valid data") ;}@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {for (Object obj: args) {CheakValue (obj);} try {return method. invoke (target, args);} catch (Exception e) {e. printStackTrace ();} return null ;}}
The two above are proxy objects. The nested proxy can be implemented using the following code:
ShowLogHanlder.initNextAngient(ChackValueHanlder.initNextAngient(calucateImpl))