Proxy reference core Java but feel the author of this part is not well written and then come back to modify
You can use a proxy to create a class that implements an interface at run time. If you cannot determine which interface to implement at compile time, use the proxy, which is why the proxy is generated. This is not a common scenario in application development, and there are many benefits for system-level applications.
1 when to use a proxy
Join we want to construct an object, and this object implements one or more interfaces, but at compile time it is not possible to determine what the class should be. This is a complex problem, in order to create an actual object, we can simply use the Newinstance method to find a constructor using reflection. But we cannot instantiate an interface, and we need to define a class at run time by implementing that interface.
To overcome this difficulty, many programs generate code, put them in a file, start the compiler, and load the resulting class file. Naturally, this is slow and requires the compiler to deploy with our program. The proxy mechanism is a better solution. The proxy class creates a new class at run time. Such a proxy class implements the interface that we define. In particular, this proxy class has the following methods:
1 implements all the methods required by the interface
2 inherited the method of Objcet
However, we cannot add code to these methods at run time. Instead we need to provide a call processor (invoke handler). The calling processor is an instance object that implements the invocation handler interface. The interface has only one method:
Whenever the method of this proxy object is called, then the Invoke method of the calling processor is called, and we need to call Method.invoke (actual object, parameter) internally to determine the return value. Each invocation is shared by an invoke where it is to be logically judged internally
Take a look at an example
Package AA;
Interface Isomeservice {
String Dofirst ();
void Dosecond ();
}
Class Someserviceimpl implements isomeservice{
@Override
Public String Dofirst () {
System.out.println ("do First");
return "result";
}
@Override
public void Dosecond () {
System.out.println ("Do Second");
}
}
Package Aa;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.Proxy; Public classMyTest { Public Static voidMain (string[] args) {isomeservice target=NewSomeserviceimpl (); //using the proxy dynamic proxy of the JDK requires the target class to implement the interface//because the underlying execution principle is the same as the static proxyIsomeservice service= (Isomeservice) proxy.newproxyinstance (Target.getclass (). getClassLoader ()//class loader for the target class, Target.getclass (). Getinterfaces ()//all interfaces implemented by the target class,NewInvocationhandler () {//Anonymous Inner class//Proxy: Agent Object//method: Target Approach//args: Parameter list of the target method@Override Publicobject Invoke (Object proxy, Method method, object[] args) throws Throwable {//calling the target methodSystem. out. println ("Hei"); Object result=Method.invoke (target, args); if(result!=NULL) {result=(String) result). toUpperCase (); } returnresult; } }); String result=Service.dofirst (); System. out. println ("result="+result); Service.dosecond (); }}
Results:
2 Creating a Proxy object
In order to create a proxy object, using the Newproxyinstance method of the proxy class, this method has three parameters
1 ClassLoader: As part of the Java security mechanism, different class loaders can be used for system classes, classes downloaded from the network, and so on.
2 The interface that the proxy class needs to implement (typically the method that the interface is implemented by the proxy class)
3 Calling the processor
There are two questions: How do we define this processor? What can we do with proxy objects?
Proxies can be used for many purposes, such as:
1 ways to route a remote server
2 Connect the action-related user interface events in the running program.
3 The Tracking method executes in the mode of the purpose.
The agent for Java