01-动态代理入门演示

1 接口代码

package www.test.proxy;

public interface  TargetInterface {

	public void method1();
	public String method2();
	public int method3(int x);
}

 2 实现接口类的代码

package www.test.proxy;

public class Target implements TargetInterface {

    @Override
    public void method1() {
        System.out.println("method1 running..........");
    }

    @Override
    public String method2() {
        System.out.println("method2 running...........");
        return "method2";
    }

    @Override
    public int method3(int x) {
        
        return x ;
    }

}

3 动态代理代码演示1

package www.test.proxy;

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

import org.junit.Test;

public class ProxyTest {
    @Test
    public void test1(){
        //获得动态的代理对象----在运行时 在内存中动态的为 Target 创建一个虚拟的代理对象
        //objProxy 是代理对象 根据参数确定到底是谁的代理对象
        TargetInterface objProxy =  (TargetInterface) Proxy.newProxyInstance(
            //loader :与目标对象相同的类加载器
            Target.class.getClassLoader(),
            // interfaces :代表与目标对象实现的所有接口字节码对象数组
            new Class[]{TargetInterface.class},
            //h: 具体的操作,InvocationHandler接口
            new InvocationHandler() {
                //invoke 代表的是执行代理对象的方法
                //method: 代表目标对象的方法字节码对象
                //args:代表目标对象的相应的方法的参数
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("目标方法前的逻辑");
                    //执行目标对象的方法
                    Object invoke = method.invoke(new Target(), args);
                    System.out.println("目标方法后的逻辑");
                    return invoke;
                }
            });
        objProxy.method1();
        String method2 = objProxy.method2();
        System.out.println(method2);
        int method3 = objProxy.method3(18);
        System.out.println(method3);
    }
}

4 动态代理演示代码2

package www.test.proxy;

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

import org.junit.Test;

public class ProxyTest2 {
    @Test
    public void test1(){
        final Target target = new Target();
        //动态创建代理对象
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                //Proxy.newProxyInstance(loader, interfaces, h)
                //loader: 代表与目标对象相同的类加载器
                target.getClass().getClassLoader(),
                // interfaces :代表与目标对象实现的所有接口字节码对象数组
                target.getClass().getInterfaces(), //返回值为字节码对象数组
                // h : 具体的操作,InvocationHandler接口
                new InvocationHandler() {
                    @Override
                    //被执行几次? ------- 看代理对象调用方法几次
                    //代理对象调用接口相应方法 都是调用 invoke
                    /*
                    * proxy:是代理对象
                    * method:代表的是目标方法的字节码对象
                    * args:代表是调用目标方法时参数
                    */
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //反射知识点
                        Object invoke = method.invoke(target, args);//目标对象的相应方法
                        //retrun 返回的值给代理对象
                        return invoke;
                    }
                });
        //调用 invoke---Method: 目标对象的 method1 方法 args: null 返回值 null
        proxy.method1();
        //调用 invoke---Method:目标对象的 method2 方法 args:null 返回值 method2
        String method2 = proxy.method2();
        //调用 invoke-----Method:目标对象的 method3 方法 args:Object[]{100} 返回值 100
        int method3 = proxy.method3(100);
        System.out.println(method2);
        System.out.println(method3);
        
    }
}
原文地址:https://www.cnblogs.com/jepson6669/p/8438044.html