Explanation and demonstration of dynamic proxy interfaces reflected in Java
Explanation and demonstration of dynamic proxy interfaces reflected in Java
Java introduced a dynamic proxy mechanism in JDK1.3, which can be implemented by capturing events, reviewing data, logs, and other functions in framework programming and platform programming. First, let's take a look at the UML diagram of the design mode:
When you call an interface API, the actual implementation class inherits the interface and is implemented by proxy during the call.
Two key interface classes and class classes implemented by dynamic proxy in Java are as follows:
Java. lang. reflect. Proxy
Java. lang. reflect. InvocationHandler
The following describes how to use the InvocationHandler interface to implement the dynamic Proxy process.
A proxy class, and then test the complete program. The following steps are required to implement the Demo:
I. First, define our own POJO object interface class IExample and IUser
package com.example.pojo;public interface IExample {public void setName(String name);public String getName();public void setDesc(String description);public String getDesc();}
package com.example.pojo;public interface IUser {public void setUserID(String userID);public String getUserID();public void setUserName(String userName);public String getUserName();}2. implement our own InvocationHandler interface, where map is used to store the data of POJO objects. The advantage of doing so is that the POJO interface does not need to create implementation classes, this class can be used directly through the proxy only through the defined interface, which is very useful in actual project development.
package com.example.reflection;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.HashMap;import java.util.Map;public class MyProxyView implements InvocationHandler {private Map
map = null;public static Object newInstance(Class[] interfaces) {return Proxy.newProxyInstance(MyProxyView.class.getClassLoader(),interfaces, new MyProxyView());}private MyProxyView() {this.map = new HashMap
();}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {Object result = null;String methodName = method.getName();if (methodName.startsWith("get")) {String name = methodName.substring(methodName.indexOf("get") + 3);return map.get(name);} else if (methodName.startsWith("set")) {String name = methodName.substring(methodName.indexOf("set") + 3);map.put(name, args[0]);return null;} else if (methodName.startsWith("is")) {String name = methodName.substring(methodName.indexOf("is") + 2);return (map.get(name));}return result;}}
3. initialize the Proxy to obtain the POJO object, run and test:
package com.example.reflection;import com.example.pojo.IExample;import com.example.pojo.IUser;public class TextProxy {public static void main(String[] args){IExample example = (IExample)MyProxyView.newInstance(new Class[]{IExample.class});IUser user = (IUser)MyProxyView.newInstance(new Class[]{IUser.class});// aduit bean 1example.setName("my example");example.setDesc("my proxy example");// aduit bean 2user.setUserID("jia20003");user.setUserName("gloomyfish");System.out.println("exmaple name : " + example.getName());System.out.println("exmaple desc : " + example.getDesc());System.out.println();System.out.println("user ID : " + user.getUserID());System.out.println("user name : " + user.getUserName());}}4. The running result is as follows:
exmaple name : my exampleexmaple desc : my proxy exampleuser ID : jia20003user name : gloomyfish
The Java Dynamic proxy method is very important for framework programming, both on the Web side and on the desktop side.
The spring framework is used to develop this technology.