The Java dynamic proxy class is located under the java.lang.reflect package, and generally mainly involves the following two classes:
(1) InvocationHandler: Only one method is defined in this interface
public object invoke(Object obj,Method method, Object[] args)
In actual use, the first parameter obj generally refers to the proxy class, method is the proxy method, such as request() in the above example, args is the parameter array of the method. This abstract method is implemented dynamically in the proxy class.
(2) Proxy: This class is a dynamic proxy class, and its function is similar to the ProxySubject in the above example, which mainly contains the following contents protected Proxy(InvocationHandler h): a constructor, used to assign values to the internal h.
static Class getProxyClass (ClassLoader loader, Class[] interfaces)
Get a proxy class where loader is the class loader and interfaces is an array of all interfaces owned by the real class.
static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)
Returns an instance of the proxy class. The returned proxy class can be used as the proxy class (the method declared in the Subject interface of the proxy class can be used)
The so-called Dynamic Proxy is a class: it is a class generated at runtime. When generating it, you must provide a set of interfaces to it, and the class claims that it implements these interfaces. Of course you can use the instance of the class as any of these interfaces. Of course, this Dynamic Proxy is actually a Proxy, which will not do substantial work for you. When generating its instance, you must provide a handler and it will take over the actual work.
When using dynamic proxy classes, we must implement the InvocationHandler interface:
See the program Subject.java
See the program RealSubject.java
See the program DynamicSubject.java
See the program Client.java
The code copy is as follows:
package com.langsin.dynamicproxy;
//Abstract role (previously it was an abstract class, it should be changed to an interface here):
public interface Subject
{
abstract public void request();
}
The code copy is as follows:
package com.langsin.dynamicproxy;
//Specific role
public class RealSubject implements Subject
{
public RealSubject()
{
}
public void request()
{
System.out.println("From real subject.");
}
}
The code copy is as follows:
package com.langsin.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
//Proxy Processor
/**
* The internal properties of the proxy class are Object class. When used, it is assigned a value through the constructor of the class DynamicSubject(Object obj);
* In addition, the invoke method is also implemented in this class, method.invoke(sub,args);
* In fact, it is to call the method to be executed by the proxy object. The method parameter sub is the actual proxy object.
* args is the parameter required to perform the corresponding operation of the proxy object.
* Through dynamic proxy classes, we can perform some related operations before or after the call
*/
public class DynamicSubject implements InvocationHandler
{
private Object sub;
public DynamicSubject()
{
}
public DynamicSubject(Object obj)
{
sub = obj;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
System.out.println("before calling " + method);
method.invoke(sub, args);
System.out.println("after calling " + method);
return null;
}
}
The code copy is as follows:
package com.langsin.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
//Client
public class Client
{
static public void main(String[] args) throws Throwable
{
RealSubject rs = new RealSubject(); // Specify the proxy class here
InvocationHandler ds = new DynamicSubject(rs);
Class<?> cls = rs.getClass();
// The following is a one-time generation agent
Subject subject = (Subject) Proxy.newProxyInstance(
cls.getClassLoader(), cls.getInterfaces(), ds);
subject.request();
}
}
Example 2:
The code copy is as follows:
package com.langsin.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Vector;
public class VectorProxy implements InvocationHandler
{
private Object proxyobj;
public VectorProxy(Object obj){
proxyobj = obj;
}
public static Object factory(Object obj){
Class<?> cls = obj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new VectorProxy(obj));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
System.out.println("before calling " + method);
if (args != null){
for (int i = 0; i < args.length; i++){
System.out.println(args[i] + "");
}
}
Object object = method.invoke(proxyobj, args);
System.out.println("after calling " + method);
return object;
}
@SuppressWarnings("unchecked")
public static void main(String[] args){
List<String> v = (List<String>) factory(new Vector<String>(10));
v.add("New");
v.add("York");
System.out.println(v);
v.remove(0);
System.out.println(v);
}
}
Example 3.
The code copy is as follows:
package com.langsin.dynamicproxy;
public interface Foo{
void doAction();
}
package com.langsin.dynamicproxy;
public class FooImpl implements Foo{
public FooImpl(){
}
public void doAction(){
System.out.println("in FooImp1.doAction()");
}
}
package com.langsin.dynamicproxy;
public class FooImpl2 implements Foo{
public FooImpl2(){
}
public void doAction(){
System.out.println("in FooImp2.doAction()");
}
}
package com.langsin.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class CommonInvocationHandler implements InvocationHandler{
// Dynamic execution of objects, objects that require callbacks
private Object target;
// Support constructor injection
public CommonInvocationHandler(){
}
// Support constructor injection
public CommonInvocationHandler(Object target){
setTarget(target);
}
/**
*
* Use setter method to inject
*
* @param target
*
*/
public void setTarget(Object target){
this.target = target;
}
/**
*
* Call the method specified in proxy and pass in the parameter list args
*
* @param proxy
* The type of the proxy class, such as defining the proxy interface corresponding to the method
*
* @param method
* Method of being proxyed
*
* @param args
* Parameters that call the proxy method
*
* @return
*
* @throws java.lang.Throwable
*
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
return method.invoke(target, args);
}
}
package com.langsin.dynamicproxy;
import java.lang.reflect.Proxy;
public class Demo{
public static void main(String[] args){
// 1. General dynamic proxy implementation
CommonInvocationHandler handler = new CommonInvocationHandler();
Foo f;
// 2. Interface implementation 1
handler.setTarget(new FooImpl());
// Method parameter description: proxy class, proxy class implementation list, proxy class processor
// Associate the proxy class, interface methods, and processors in the proxy class. When the interface methods in the proxy class are called, they will be automatically distributed to the processor's invoke method.
// If the proxy class does not implement the specified interface list, an illegal parameter exception will be thrown
f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
f.doAction();
// 3. Interface implementation 2
handler.setTarget(new FooImpl2());
f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
f.doAction();
}
}
Because I have limited literary talent, most of the content is code. Please understand ^_^