Use Jdk dynamic agent

A) the step of creating a dynamic proxy

1, the interface theme

2, Acting Class

3, real class

4, the use of class

1) a body interface defines a common interface method proxy classes and the true class of the proxy class and real class respectively, to achieve the main interface real class implements the specified logical interface method, proxy class also implement the same interface method, the method calling logic real class, got the authorization is equivalent to the agent, the agent has been executed function.

2) class practical use java polymorphic characteristics using a common interface type receiving agent, the receiving agent class object, use the public proxy class calls the method to achieve the specified logical true class.

B) using jdk dynamic proxy

First, why should use dynamic proxy?

1), the original interface corresponds to a static proxy a proxy class, when the real subject of many, to write multiple proxy class

2) When there are changes in the interface, real class and agent class that needs to change. Using a dynamic proxy, the proxy class is dynamically generated, dynamic proxy class public interface implemented method of

Second, step

1, the interface theme

2, the real category

3, InvocationHanlder interface implementation class

Proxy.newInstance(ClassLoader loader, class<?>[] inteface,          InvocationHander hanlder)   //创建一个代理实例

    invoke(Object proxy, Method method, args)//代理类的实现逻辑

4, the use of class

C) using dynamic proxy implementation delayed loading of a database query

Interface theme:

/**
 * 数据库查询的公共接口
 */
public interface DBQueryInterface {
    public String request();
}

Real categories:

public class DBQuery implements DBQueryInterface{
    /**
     * 模拟重量级对象,对象初始化时需要进行数据库连接
     */
    DBQuery(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 数据库查询的具体逻辑实现
     * @return
     */
    @Override
    public String request() {
        return "我正在执行数据库查询操作";
    }
}

Dynamic proxy class:

public class DBQueryJdkProxy implements InvocationHandler {
    DBQuery dbQuery = null;
    /**
     * 获取代理对象
     */
    public Object bind(){
        return Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{DBQueryInterface.class}, new DBQueryJdkProxy());
    }

    /**
     * invoke方法是代理逻辑的具体实现
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(dbQuery == null){
            //延迟加载,当使用到时才创建重量级对象
            dbQuery = new DBQuery();
        }
       return dbQuery.request();
    }
}

Use categories:

public class Client {
    public static void main(String[] args) {
        //初始化时先加载代理对象
        DBQueryInterface JdkProxy= (DBQueryInterface) new DBQueryJdkProxy().bind();
        //使用时才创建真实的对象
        String result = JdkProxy.request();
        System.out.println(result);
    }
}

result:

我正在执行数据库查询操作

Improvement: using dynamic proxy implementation lazy loading while enhancing the functionality of the original class

Interface theme:

/**
 * 数据库查询的公共接口
 */
public interface DBQueryInterface {
    public String request();
}

Real categories:

public class DBQuery implements DBQueryInterface{
    /**
     * 模拟重量级对象,对象初始化时需要进行数据库连接
     */
    DBQuery(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 数据库查询的具体逻辑实现
     * @return
     */
    @Override
    public String request() {
        return "我正在执行数据库查询操作";
    }
}

Dynamic proxy class:

/**
 * 使用动态代理创建数据库查询的代理对象
 * 1、实现InvocationHandler接口
 * 2、调用Proxy.newInstance()动态的创建代理对象
 */
public class DBQueryJdkProxy implements InvocationHandler {
    Object target;
    /**
     * 获取代理对象
     */
    public Object bind(Object target){//target是真实类对象,给给定的真实类对象创建一个代理,由代理去完成真实类的工作
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    /**
     * 增强原有类的功能
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("事物开始");
        System.out.println(method.invoke(target, args));
        System.out.println("事物结束");
        return null;
    }
}

Use categories:

public class Client {
    public static void main(String[] args) {
        //加载数据库查询代理工厂
        DBQueryJdkProxy dbQueryJdkProxy = new DBQueryJdkProxy();
        //给指定的对象创建代理工厂
        DBQueryInterface proxy = (DBQueryInterface) dbQueryJdkProxy.bind(new DBQuery());
        proxy.request();
    }
}

Guess you like

Origin www.cnblogs.com/Auge/p/11579271.html