In this paper, the Java Dynamic Proxy for Knowledge point collation, the specific contents are as follows
A. Java dynamic Proxy (more official version)
Proxy mode is a common Java design pattern, his characteristic is that the proxy class has the same interface with the delegate class, the proxy class is mainly responsible for preprocessing the message for the delegate class, filtering the message, forwarding the message to the delegate class, and handling the message afterwards. There is usually an association between a proxy class and a delegate class, the object of a proxy class is associated with an object of a delegate class, and the object of the proxy class does not actually implement the service, but rather provides a specific service by invoking the methods associated with the object of the delegate class.
According to the creation period of the agent, the proxy class can be divided into two kinds.
static agents: created by a programmer or by a specific tool, the source code is automatically generated, and then compiled. Before the program runs, the. class file for the proxy class already exists.
Dynamic Agent: when the program is running, it is created dynamically by using the reflection mechanism.
Two. Dynamic Proxy implementation
Java.lang.reflect.Proxy,
Proxy provides static methods for creating dynamic proxy classes and instances.
Newproxyinstance ()
Returns a proxy class instance of the specified interface that can assign a method call to the specified call handler.
2.1. DAO interface (provides analog data access layer interface)
Package javaproxy;
/* Define a data Access layer interface
*
/public interface Dao
{//Analog data Save public
void Save ();
}
2.2. Daoimpl class Implementation Class
Package javaproxy;
The public class Daoimpl implements dao{is
@Override public
Void Save () {
System.out.println ("Execute a Save Method ...). ");
}
}
2.3. Agent Processing class
Package javaproxy;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
public class Daohandler implements invocationhandler{
private Object obj;
Public Daohandler (Object obj) {
this.obj=obj
}
@Override Public
object Invoke (Object proxy, Method method, object[] args)
throws Throwable {
System.out.println ("Do something before Methods");//Here Simulate AOP's predecessor method
Object ret = Method.invoke (this.obj, args);
System.out.println ("Do something after Methods");//Here Simulate AOP's back-up method return
ret;
}
2.4. Client invocation
Package javaproxy;
Import Java.lang.reflect.Proxy;
public class Client {public
static void Main (string[] args) {
//Meta object (proxy object)
Daoimpl Daoimpl = new Daoimpl (); c6/>//Business Agent class
Daohandler daohandler=new Daohandler (Daoimpl);
DAO dao= (DAO) proxy.newproxyinstance (Daoimpl. GetClass (). getClassLoader ()
, Daoimpl.getclass ()
. Getinterfaces (), daohandler);
Dao.save ();
}
2.5. Results
3. Proxy implementation in analog MyBatis
3.1. Mapperproxy class
Package javaproxy;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy; public class Mapperproxy implements Invocationhandler {@SuppressWarnings ("unchecked") * * Here is the object of the implementation class through a static method * * @param: Interface * * @param: Execute method with sqlsession * * @return: Return proxy pair like/public static <T> T Newmapperproxy (class< ;
T> Mapperinterface, Object sqlsession) {ClassLoader ClassLoader = Mapperinterface.getclassloader ();
Class<?>[] interfaces = new class[] {mapperinterface};
Mapperproxy proxy = new Mapperproxy ();
Return (T) proxy.newproxyinstance (ClassLoader, interfaces, Proxy); }/* * (NON-JAVADOC) * * @see Java.lang.reflect.invocationhandler#invoke (java.lang.Object, * java.lang.reflect.
method, java.lang.object[]) * * @param: Proxy Object * * @param: Methods to find the corresponding method through the name of the method * * @param: Find the corresponding pass parameter mapping by means of passing in the object *
* @return: Returns the Parameter object after execution/public object invoke (object proxy, Method method, object[] args) Throws Throwable {//Here the corresponding operation is performed by resolving the corresponding mapper by means of the method name and Parameters System.out.println ("Implement the actual method here");
return null;
}
}
3.2. Client
Package javaproxy;
Import Java.lang.reflect.Proxy;
public class Client {public
static void Main (string[] args) {
Dao dao=mapperproxy.newmapperproxy (Dao.class, NULL);
Dao.save ();
}
3.3. The result
These are examples of using JDK dynamic proxies to help you learn Java programming.