This article organizes knowledge points for Java dynamic agents, the specific content is as follows
1. JAVA dynamic proxy (comparative official statement)
The proxy model is a commonly used Java design model. Its characteristic is that the proxy class and the delegate class have the same interface. The proxy class is mainly responsible for preprocessing messages, filtering messages, forwarding messages to the delegate class, and processing messages after the event. There is usually an association between the proxy class and the delegate class. The object of a proxy class is associated with the object of a delegate class. The object of the proxy class itself does not truly implement the service, but provides specific services by calling the relevant methods of the delegate class object.
According to the period of agent creation, agent classes can be divided into two types.
Static proxy: Created by programmers or automatically generates source code by specific tools and compiles it. Before the program runs, the .class file of the proxy class already exists.
Dynamic proxy: It is created dynamically using reflection mechanism when the program is running.
2. Dynamic proxy implementation
java.lang.reflect.Proxy,
Proxy provides static methods for creating dynamic proxy classes and instances.
newProxyInstance()
Returns an instance of the proxy class of the specified interface that can assign method calls to the specified call handler.
2.1. Dao interface (provides simulated data access layer interface)
package javaproxy;/* * Define a data access layer interface*/public interface Dao { //Simulate data saving public void save();} 2.2. DaoImpl class implementation class
package javaproxy; public class DaoImpl implements Dao{ @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 method");//The pre-method of AOP is simulated here Object ret = method.invoke(this.obj, args); System.out.println("do something after method");//The post-method of AOP is simulated here return ret; } } 2.4. Client call
package javaproxy; import java.lang.reflect.Proxy; public class Client { public static void main(String[] args) { // Metaobject (proxed object) DaoImpl daoImpl = new DaoImpl(); // Business proxy class DaoHandler daoHandler=new DaoHandler(daoImpl); Dao dao=(Dao) Proxy.newProxyInstance(daoImpl .getClass().getClassLoader(), daoImpl.getClass() .getInterfaces(), daoHandler); dao.save(); }}2. 5. Results
3. Simulate the proxy implementation in 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 we get the object of the implementing class through static methods* * @param: Interface* * @param: Execute method with sqlsession* * @return: Return proxy object*/ 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: method finds the corresponding method through the method name* * @param: pass the object into the object through the method to find the corresponding parameter map* * @return: Return the executed parameter object*/ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Here, the corresponding operation is performed by parsing the corresponding mapper through the method name and parameters System.out.println("Execute 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. Results
The above is an example of using JDK dynamic proxy. I hope it will be helpful for everyone to learn Java programming.