Dynamic agent of reflection mechanism

Keywords: Java

Dynamic proxy

Static proxy

Agent design: the interface of an operation has two subclasses, one is the implementation class of the real topic, the other is the agent class. The agent implementation class needs to complete more content than the implementation class of the real topic, and it also needs to handle some program codes related to specific business.

package dyncprocy;

public interface Subject {
	public String say(String name,int age);//Define abstract method say
}

package dyncprocy;

public class RealSubject implements Subject{	//Implementation interface

	@Override
	public String say(String name, int age) {
		
		return "Full name: " + name + "Age:" + age; 
	}
	
	
	
}

package dyncprocy;

public class ProxySubject implements Subject{
	private Subject sub = null;

	public ProxySubject(Subject sub) {
		this.sub = sub;
	}

	@Override
	public String say(String name, int age) {
		
		return this.sub.say(name, age);
	}
	
	
}

package dyncprocy;

public class DynaProxyDemo {
	public static void main(String[] args) {
		Subject sub = new ProxySubject(new RealSubject());
		String info = sub.say("yue", 17);
		System.out.println(info);
	}
}

In fact, the above code operations are static agents, because a proxy class can only serve one interface. If there are many interfaces now, there will be many proxy classes. Besides, all proxy operations, except for calling different methods, have the same operations. They must be repeated code.

So Java has a dynamic proxy mechanism.

Dynamic proxy

You can think of the subclass of the invocationHandler interface as the final operation class of an agent, replacing the ProxySubject.

public class MyInvocationHandler implements InvocationHandler{

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		
		return null;
	}

}

ClassLoader represents the loader of a class. There are generally three types of loaders in java

If you want to get a loader object, you must use Class

package dyncprocy;

class p1{
	
}
public class ClassLoaderDemo {
	
	public static void main(String[] args) {
		p1 p = new p1();
		System.out.println("Class loader:" + p.getClass().getClassLoader().getClass().getName());;
	}
}

Dynamic agent:

package dyncprocy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvocationHandler implements InvocationHandler{
	private Object obj;
	public Object bind(Object obj) {
		this.obj = obj;//Bind real theme class
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		this.before();
		Object temp = method.invoke(this.obj, args);//Calling method
		return temp;
	}
	public void before() {
		System.out.println("Before agent operation");
	}
}

Conclusion:

1. With dynamic proxy, you can complete the function of proxy and proxy all interfaces.

Posted by Blicka on Tue, 15 Oct 2019 08:04:02 -0700