当前位置:网站首页>Dynamic Agent Learning

Dynamic Agent Learning

2022-08-11 07:49:00 LiuJia111222333

动态代理和静态代理角色一样
动态代理的代理类是动态生成的,不是我们直接写好的
动态代理分为两大类:A dynamic proxy for the base interface,Dynamic proxy for base classes

There are three ways to implement dynamic proxy:

  1. 基于接口---jdk动态代理
  2. 基于类:cglib
  3. java字节码实现:javassit

需要了解两个类:Proxy(代理类),InvocationHandler (调用处理程序)   

InvocationHandler

InvocationHandler 是代理实例的调用处理程序 实现的接口.

每个代理实例都具有一个关联的调用处理程序.对代理实例调用方法时,将对方法调用进行编码并将其指派到它的调用处理程序的 invoke 方法.

invoke(Object proxy, Method method, Object[] args)

参数解析:

proxy- 调用该方法的代理实例

method-MethodInstance corresponding to the interface method invoked on the proxy instance.The declared class of the objectMethodwill be the interface declaring the method,It may be a superinterface of the proxy interface from which the proxy class inherits methods.

args- 包含在代理实例上的方法调用中传递的参数值的对象数组,或者nullIf the interface method does not accept any parameters.Parameters of primitive types are wrapped in an instance of the appropriate primitive wrapper class,例如 java.lang.Integeror java.lang.Boolean.

ProxyCreate a static proxy class

     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                                          new Class<?>[] { Foo.class },
                                          handler);

参数解析:

loader- 定义代理类的类加载器

interfaces- 代理类要实现的接口列表

h- 将方法调用分派到的调用处理程序

 学习案例1:

Rent接口

//租房接口
public interface Rent {
    public void rent(); //租房
}

Host房东类

//The landlord implements the rental interface,It means he wants to rent a house
public class Host implements Rent {

    @Override
    public void rent() {
        System.out.println("The landlord needs to rent out the house!");
    }
}

ProxyInvocationHandler 处理程序类 

//等我们会用这个类,自动生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
    //创建被代理的接口
    private Rent rent;

    public void setRent(Rent rent){
        this.rent = rent;
    }

    //生成得到的代理类
    public Object getProxy(){
        //Create a proxy class
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(),this);
    }

    //处理代理实例,并返回结果,When the implementation interface is,系统会默认执行invoke处理程序
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //Executive landlord rents out the house
        Object result = method.invoke(rent, args);
        seeHouse();
        SignContract();
        return result;
    }

    //处理其他业务
    public void seeHouse(){
        System.out.println("中介带看房子");
    }
    //处理其他业务
    public void SignContract(){
        System.out.println("中介签合同");
    }
}

主类

public class Client {
    public static void main(String[] args) {
        //The real character is the landlord,He needs to find an agent to rent her a house
        Host host = new Host();

        //There is no proxy role for the time being,So we need to create
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        pih.setRent(host); //Setting up a landlord requires an agent
        //A proxy class for a landlord to rent a house has been created.
        Rent proxy = (Rent) pih.getProxy();
        //Executive rental
        proxy.rent();
    }

}

学习案例2 -- Universal dynamic proxy:

UserService接口

public interface UserService {
    void add();
    void update();
    void delete();
    void select();
}

UserServiceImpl实现类:

public class UserServiceImpl implements  UserService{
    @Override
    public void add() {
        System.out.println("使用了add方法 ");
    }

    @Override
    public void update() {
        System.out.println("使用了update方法 ");
    }

    @Override
    public void delete() {
        System.out.println("使用了delete方法 ");
    }

    @Override
    public void select() {
        System.out.println("使用了select方法 ");
    }
}
ProxyInvocationHandler类:
​
//Create a universal dynamic proxy
//等我们会用这个类,自动生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
    //创建被代理的接口
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //生成得到的代理类
    public Object getProxy() {
        //Create a proxy class
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    //处理代理实例,并返回结果,When the implementation interface is,系统会默认执行invoke处理程序
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //Dynamically get the name of the method
        System.out.println("debug提示:"+"使用了"+method.getName()+"方法");
        //Executive landlord rents out the house
        Object result = method.invoke(target, args);

        return result;
    }
}

​

主类:

public class Client {
    public static void main(String[] args) {
        //创建一个真实角色
        UserService userService = new UserServiceImpl();

        //Generate a proxy stunning
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        pih.setTarget(userService);
        //创建代理类
        UserService proxy = (UserService) pih.getProxy();
        proxy.delete();//执行删除方法

    }

}

原网站

版权声明
本文为[LiuJia111222333]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110646080068.html