一劳永逸结局动态代理

java的包结构

 

接口

public interface Tar_Hello_Interface {
    public String sayHello(String param);
    public String sayHello1(String param,int a);
    public void sayHello2(String param, String b);
}

接口实现类

public class Tar_Hello implements Tar_Hello_Interface{
    @Override
    public String sayHello(String param){
        return  "Tar_Hello:"+param;
    }
​
    @Override
    public String sayHello1(String param, int a) {
        return "Tar_Hello:"+param;
    }
​
    @Override
    public void sayHello2(String param, String b) {
        System.out.println("Tar_Hello:"+param + b);
    }
}

静态代理实现

public class StaticDaiLi {
    private Tar_Hello_Interface tar_hello_interface;
​
    public StaticDaiLi() {
    }
​
    public Tar_Hello_Interface getTar_hello_interface() {
        return tar_hello_interface;
    }
​
    public void setTar_hello_interface(Tar_Hello_Interface tar_hello_interface) {
        this.tar_hello_interface = tar_hello_interface;
    }
​
    public StaticDaiLi(Tar_Hello_Interface tar_hello_interface) {
        this.tar_hello_interface = tar_hello_interface;
    }
    //把方法名重写
    public  String sayHello(String param){
        String str = null;
​
        System.out.println("日志开始打印。。。。。");
​
        str = tar_hello_interface.sayHello(param);
​
        System.out.println(str);
​
        System.out.println("日志结束打印。。。。。");
​
        return str;
    }
}
​
​
​

JDK动态代理实现

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
​
public class JDKDaiLi implements InvocationHandler{
​
    private Object tar_Class;
    
    /*
    * jdk代理实现是继承目标类接口进行实现的
    * */
    public JDKDaiLi() {
​
    }
​
​
    public Object getNewInstance(Object tar_Class){
        this.tar_Class = tar_Class;
        Object o = Proxy.newProxyInstance(
                //利用反射获取类加载器
                tar_Class.getClass().getClassLoader(),
                //把该代理类被现实于对应的代理类的实现接口类上
                tar_Class.getClass().getInterfaces(),
                //方法中断处理器,反射机制提供的
                this
        );
​
​
        //返回代理类
        return o;
    }
​
​
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
​
        System.out.println("日志开始打印。。。。。");
​
​
        System.out.println(method.getName());
        for (Object arg : args) {
​
            //接口中不同方法的拥有的的参数
            System.out.println(arg);
        }
        Object o = method.invoke(tar_Class, args);
​
        System.out.println("日志结束打印。。。。。");
​
        //返回代理对象的返回值,对象:对象,void:null
        return o;
    }
}

Cglib动态代理实现

(先进行导包 org.mockito.cglib.proxy.Enhancer)

import org.mockito.cglib.proxy.Enhancer;
import org.mockito.cglib.proxy.InvocationHandler;
import org.mockito.cglib.proxy.MethodInterceptor;
import org.mockito.cglib.proxy.MethodProxy;
​
import java.lang.reflect.Method;
​
public class CglibDaiLi {
​
​
    private Object tar_Class ;
​
​
    /*
    * 代理实现的是子类实现
    * */
    public Object getNewInstance(Object target){
​
        tar_Class = target;
​
        //创建增强器
        Enhancer enhancer = new Enhancer();
​
        /*
        * 设置实现的代理的超类
        * */
        enhancer.setSuperclass(target.getClass());
​
        /*
        * 设置回调函数,和jdk类似,实现jdk实现InvocationHandler接口
        *                   cglib实现MethodInterceptor接口
        * */
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
​
                System.out.println("日志开始打印。。。。。");
​
​
                System.out.println(method.getName());
​
​
​
                Object invoke = proxy.invokeSuper(obj, args);
​
                System.out.println("日志结束打印。。。。。");
                return invoke;
            }
        });
​
        //从增强器创建对应的代理对象
        Object o = enhancer.create();
        //返回
        return o;
​
    }
}

Main

public class Main {
    public static void main(String[] args) {
        /*
        * 静态代理实现
        * */
//        Tar_Hello_Interface tar_hello_interface = new Tar_Hello();
//
//        StaticDaiLi staticDaiLi = new StaticDaiLi(tar_hello_interface);
//
//        String s = staticDaiLi.sayHello("world");
//
//        System.out.println(s);
​
​
        /*
        * jdk动态代理
        *
        * */
//        JDKDaiLi jdkDaiLi = new JDKDaiLi();
//
//        Object newInstance = jdkDaiLi.getNewInstance(new Tar_Hello());
//        Tar_Hello_Interface tar_hello_interface = (Tar_Hello_Interface) newInstance;
//
//        String s = tar_hello_interface.sayHello("你好");
//        tar_hello_interface.sayHello1("中国",2019);
//        tar_hello_interface.sayHello2("chencj","cc");
​
​
        /*
        * cglib动态代理
        *
        * */
        CglibDaiLi cglibDaiLi = new CglibDaiLi();
        Tar_Hello newInstance = (Tar_Hello) cglibDaiLi.getNewInstance(new Tar_Hello());
        newInstance.sayHello("你好");
        newInstance.sayHello1("中国",2019);
        newInstance.sayHello2("chencj","cc");
​
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_22988423/article/details/87547467