Static Proxy:
Defining the top-level interface: IService
// both the target class and the proxy class implement the interface Public Interface IService { public String server ();}
Define target class: Serviceimp implement IService interface
// Target Class Public class Implements IService { @Override public String Server () { return "Hello" ; }}
Define the proxy class: Serviceproxy implements the IService interface to change the return value of the target method in the target class to uppercase characters
// proxy class Public class Implements IService { iservice target; Public Serviceproxy () { } public serviceproxy (iservice target) { this . target = target; } @Override public String Server () { return target.server (). toUpperCase (); }}
Test:
Public class Test { publicstaticvoid main (string[] args) { iservice target= New serviceimp (); IService Proxy=new serviceproxy (target); System.out.println (Proxy.server ());} }
Two ways to implement dynamic agents
1, using the proxy of the JDK to implement the agent, requires the target class and the proxy class to implement the same interface. If no interface exists for the target class, it cannot be implemented in this way.
2, for non-interface classes, you need to use Cglib to implement the proxy. The generation principle of the cglib agent is to generate a subclass of the target class, and the subclass is enhanced, and the subclass is the proxy object. Therefore, using cglib to generate dynamic proxies requires that the target class must be inherited, that is, a class that cannot be a final decoration.
CGLIB (Code Generation Library) is an open source project that is a powerful, high-performance, high-quality code-generation class library. It can extend and enhance Java classes during run time. Hibernate uses it to implement the dynamic generation of bytecode for persistent objects, and spring uses it to implement AOP programming. The bottom of the Cglib package is to convert bytecode and generate new classes by using the bytecode processing framework ASW (Java Bytecode processing framework). The cglib is generated by an enhancement to the bytecode.
Related Instance code:
1, Proxy implementation:
Public classTest { Public Static voidMain (string[] args) {//Defining target ObjectsIService target=NewServiceimp (); //to define a proxy object for the target objectIService myproxy=(IService) proxy.newproxyinstance (Target.getclass (). getClassLoader (),//class loader for the target classTarget.getclass (). Getinterfaces (),//interfaces implemented by the target class NewInvocationhandler () {//Proxy: Agent Object//method: Target Approach//args: Parameters of the target method@Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Object result=Method.invoke (target, args); if("Server". Equals (Method.getname ())) {Result=result.tostring (). toUpperCase (); } returnresult; } }); System.out.println (Myproxy.server ()); }}
2, Cglib realization:
Define target class: Service
Public class Service { public String Server () { return ' Hello '; }}
To create the Cglib agent:
ImportJava.lang.reflect.Method;ImportNet.sf.cglib.proxy.Enhancer;ImportNet.sf.cglib.proxy.MethodInterceptor;ImportNet.sf.cglib.proxy.MethodProxy; Public classCglibproxyImplementsMethodinterceptor {PrivateService Target; PublicCglibproxy () {} PublicCglibproxy (Service target) { This. target=Target; } //Create a Cglib proxy object PublicService myproxy () {Service service=NewService (); Enhancer Enhancer=Newenhancer (); //Specifies the parent class, which is the target class to be enhancedEnhancer.setsuperclass (Service.class); //specifying callback interface ObjectsEnhancer.setcallback ( This); //Create a Cglib proxy object return(Service) enhancer.create (); } @Override PublicObject Intercept (Object obj, Method method, object[] args, Methodproxy proxy)throwsthrowable {Object result=Method.invoke (target, args); if("Server". Equals (Method.getname ())) {Result=result.tostring (). toUpperCase (); } returnresult; }}
Test:
Public class Test { publicstaticvoid main (string[] args) { // To Create a target object Service target=new Service (); // Create a proxy object for the target object Service proxy=New cglibproxy (target). myproxy (); System.out.println (Proxy.server ());} }
Java Advanced One: Proxy and dynamic proxy