• Java中两个动态代理


    Java实现动态代理的两种方式

    Java领域中,常用的动态代理实现方式有两种,一种是利用JDK反射机制生成代理,另外一种是使用CGLIB代理。

    JDK代理必须要提供接口,而CGLIB则不需要,可以直接代理类。下面分别举例说明。

    1.JDK动态代理:

    public interface People {
        public void sayHello();
    }
    复制代码
    复制代码
    public class Chinese implements People {
    
        @Override
        public void sayHello() {
            System.out.println("Chinese say hello.");
        }
    }
    复制代码
    复制代码
    复制代码
    复制代码
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    public class PeopleInvocationHandler implements InvocationHandler{
        
        private Object peolple;
        
        Intermediary(Object people){
            this.people = people;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {

    Object invoke = method.invoke(people, args);
            System.out.println("-------- end ---------");
            return invoke;
        }
    }
    复制代码
    复制代码
    复制代码
    复制代码
    import java.lang.reflect.Proxy;
    
    public class Test {
        public static void main(String[] args) {
            People chinese = new People();
            PeopleInvocationHandler invocationHandler = new PeopleInvocationHandler(chinese);
            People proxy = (People) Proxy.newProxyInstance(chinese.getClass().getClassLoader(), chinese.getClass().getInterfaces(), invocationHandler);
            proxy.sayHello();
        }
    }
    复制代码
    复制代码

    2.CGLIB动态代理

    需要引入CGLIB相关Jar包

    public class Chinese {
        public void sayHello(){
            System.out.println("Chinese say hello");
        }
    }
    复制代码
    复制代码
    import java.lang.reflect.Method;
    
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    
    public class ChinesePoxy implements MethodInterceptor {
    
        @Override
        public Object intercept(Object object, Method method, Object[] args,MethodProxy methodProxy) throws Throwable {
            Object intercept = methodProxy.invokeSuper(object, args);
         System.out.println("-------- end ---------");
        return intercept; 
      }
    }
    复制代码
    复制代码
    复制代码
    复制代码
    import net.sf.cglib.proxy.Enhancer;
    
    public class Test {
        public static void main(String[] args) {
            ChineseProxy chineseProxy = new ChineseProxy();
            
            Enhancer enhancer = new Enhancer();  
            enhancer.setSuperclass(Chinese.class);
            enhancer.setCallback(chineseProxy);
            
            Chinese proxy = (Chinese) enhancer.create();
            proxy.sayHello();
        }
    }

    转自:https://www.cnblogs.com/xifengxiaoma/p/9377261.html
  • 相关阅读:
    获取ip
    PHP大牛笔记收藏
    WordPress伪静态规则设置
    PHP 中 include 和 require 的区别详解
    Wordpress学习链接整理
    手机访问自动跳转
    微信web开发工具
    接入支付宝出现交易订单处理失败,请稍后再试(ALI64)的错误【转】
    HTTPS科普扫盲帖【转】
    php 好用的函数
  • 原文地址:https://www.cnblogs.com/bcl88/p/12433171.html
Copyright © 2020-2023  润新知