Article directory
1, Basic concepts
Static proxy Each proxy class can only serve one interface. If you want to complete all the proxy functions through one proxy, you must use the dynamic proxy function.
In order to implement dynamic proxy mechanism in Java, we need the support of java.lang.reflect.InvocationHandler interface and java.lang.reflect.Proxy class.
Definition of InvocationHandler interface:
public interface InvocationHandler{ public Object invoke(Onject proxy,Method method,Object[] args) throws Throwable }
This interface defines an invoke() method, which contains three parameters:
The Proxy class is a special operation class to complete Proxy. It can be used to dynamically generate implementation classes for one or more interfaces. The Proxy class provides the following operation methods:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHandler h) throws IllegalArgumentException
The newProxyInstance() method can dynamically generate implementation classes:
Class loader:
Get class loader:
class Person{} public class Root{ public static void main(String[] args) { Person stu = new Person();//Instantiate subclass object System.out.println("Class loader:" + stu.getClass().getClassLoader().getClass().getName()); } }
2, Example operation
If you want to complete the dynamic proxy, you first need to define a subclass of the InvocationHandler interface to complete the specific operation of the proxy.
- Class defining MyInvocationHandler
class MyInvocationHandler implements InvocationHandler{ private Object obj;//True theme public Object bind(Object obj){//Bind real topic operation topic this.obj = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),this);//Get proxy object } public Object invoke(Object proxy, Method method,Object[] args) throws Throwable{//Dynamic call method Object temp = method.invoke(this.obj,args);//Call methods, passing in real themes and parameters return temp; } }
Here, the bind() method of MyInvocationHandler class accepts the real subject implementation of the proxy, and then overwrites the invoke() method in the InvocationHandler interface
- Defining interfaces
interface Subject{//Define the Subject interface public String say(String name,int age);//Define abstract method say }
- Define the real theme implementation class
//Define the real theme implementation class class RealSubject implements Subject{//Real implementation class public String say(String name,int age){//Override the say() method return "Full name:" + name + ",Age:" + age;//Return information } }
The interface and the real topic class are defined here, so that the object of the real topic class can be directly passed into the bind() method of MyInvocationHandler class during operation.
- Test dynamic agents
public class Test{ public static void main(String[] args) { MyInvocationHandler handler = new MyInvocationHandler();//Instantiate agent action class Subject sub = (Subject)handler.bind(new RealSubject());//Bound object String info = sub.say("Java",30);//Calling methods through dynamic agents System.out.println(info); } }