Agent mode-dynamic agent (structured)

JDK comes with dynamic proxy

  •  java.lang.reflect.Proxy: Generate dynamic proxy classes and objects;
  •  java.lang.reflect.InvocationHandler (processor interface): can be achieved through the invoke method

RealSubject real object, register it to the processor, then create a proxy class, pass in the interface implemented by the processor and the real class, and finally you can proxy the real object

Each time the proxy class object generated by Proxy must specify the corresponding processor object. Then when the proxy class calls the method, it automatically calls the invoke method of the processor

Code:

a) Interface: Subject.java

package proxy.dynamic;

public interface Subject
{
    public int sellBooks();
 
    public String speak();
}

b) Real object: RealSubject.java 

package proxy.dynamic;

public class RealSubject implements Subject{
    @Override
    public int sellBooks() {
        System.out.println("卖书");
        return 1 ;
    }
 
    @Override
    public String speak() {
        System.out.println("说话");
        return "张三";
    }
}

c) Processor object: MyInvocationHandler.java 

package proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
 
/**
 * 定义一个处理器,里面包含了真实对象
 * @author gnehcgnaw
 * @date 2018/11/5 19:26
 */
public class MyInvocationHandler implements InvocationHandler {
    /**
     * 因为需要处理真实角色,所以要把真实角色传进来
     */
    Subject realSubject ;
 
    public MyInvocationHandler(Subject realSubject) {
        this.realSubject = realSubject;
    }
 
    /**
     *
     * @param proxy    代理类
     * @param method    正在调用的方法
     * @param args      方法的参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("调用代理类");
        if(method.getName().equals("sellBooks")){
            int invoke = (int)method.invoke(realSubject, args);
            System.out.println("调用的是卖书的方法");
            return invoke ;
        }else {
            String string = (String) method.invoke(realSubject,args) ;
            System.out.println("调用的是说话的方法");
            return  string ;
        }
    }
}

 d) Calling end: Client.java

package proxy.dynamic;

import java.lang.reflect.Proxy;
 
/**
 * 调用类
 * @author gnehcgnaw
 * @date 2018/11/7 20:26
 */
public class Client {
    public static void main(String[] args) {
        //真实对象
        Subject realSubject =  new RealSubject();
        //创建处理器,传入真实对象
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler(realSubject);
        /**
         * 利用Proxy类的静态方法newProxyInstance()创建代理对象,传入类加载器,真实类的接口,处理器
         */
        Subject proxyClass = (Subject) Proxy.newProxyInstance(
                ClassLoader.getSystemClassLoader(), 
                new Class[]{Subject.class}, 
                myInvocationHandler
        );
 
        //代理类代替真实类进行操作
        proxyClass.sellBooks();
        proxyClass.speak();
    }
}
调用代理类
卖书
调用的是卖书的方法
调用代理类
说话
调用的是说话的方法

 

Published 138 original articles · praised 34 · 150,000 views

Guess you like

Origin blog.csdn.net/bbj12345678/article/details/105274383