亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中的動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例

 更新時(shí)間:2023年12月14日 08:39:36   作者:Terisadeng  
這篇文章主要介紹了Java中的動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例,jdk動(dòng)態(tài)代理本質(zhì)上是使用被代理對(duì)象的類加載器,通過被代理類實(shí)現(xiàn)的接口在運(yùn)行時(shí)動(dòng)態(tài)構(gòu)造出代理類來增強(qiáng)原始類的功能的方法,需要的朋友可以參考下

一、JDK動(dòng)態(tài)代理

jdk動(dòng)態(tài)代理本質(zhì)上是使用被代理對(duì)象的類加載器,通過被代理類實(shí)現(xiàn)的接口在運(yùn)行時(shí)動(dòng)態(tài)構(gòu)造出代理類來增強(qiáng)原始類的功能的方法,需要使用Java的反射機(jī)制,通過實(shí)現(xiàn)InvocationHandler接口實(shí)現(xiàn)JDK動(dòng)態(tài)代理。

1、先定義一個(gè)接口

package com.teriste.fanshe;
 
public interface HelloWorld {
    void sayHello(String name);
}

2、定義被代理類

該類實(shí)現(xiàn)了接口

package com.teriste.fanshe;
 
public class HelloWorldImpl implements HelloWorld {
    @Override
    public void sayHello(String name) {
        System.out.println("Hello "+name);
    }
}

3、定義代理類

實(shí)現(xiàn)InvocationHandler接口

package com.teriste.fanshe;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
 * JDK動(dòng)態(tài)代理
 * 代理類必須實(shí)現(xiàn)了接口
 */
public class JdkProxyHandler implements InvocationHandler {
    //被代理的真實(shí)對(duì)象
    private Object target = null;
    /**
     * 建立代理對(duì)象和真實(shí)對(duì)象的代理關(guān)系,并返回代理對(duì)象
     * @param target 真實(shí)對(duì)象
     * @return 代理對(duì)象
     */
    public Object bind(Object target){
        this.target = target;
        //真實(shí)對(duì)象的類加載器 真實(shí)對(duì)象實(shí)現(xiàn)的接口 實(shí)現(xiàn)方法邏輯的代理類
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }
    /**
     * 代理方法邏輯
     * @param proxy 代理對(duì)象
     * @param method 真實(shí)對(duì)象被調(diào)用的方法
     * @param args 方法入?yún)?
     * @return 代理結(jié)果返回
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("進(jìn)入代理邏輯方法");
        System.out.println("在調(diào)度真實(shí)對(duì)象前的服務(wù)");
        Object object = method.invoke(target,args);
        System.out.println("在調(diào)度真實(shí)對(duì)象后的服務(wù)");
        return object;
    }
    public static void main(String[] args) {
        JdkProxyHandler jdkProxyHandler = new JdkProxyHandler();
        //通過真實(shí)對(duì)象和代理對(duì)象綁定,返回一個(gè)代理對(duì)象,這個(gè)對(duì)象和真實(shí)對(duì)象實(shí)現(xiàn)了相同的接口
        HelloWorld helloWorld = (HelloWorld) jdkProxyHandler.bind(new HelloWorldImpl());
        //該接口調(diào)用的方法就是代理對(duì)象的invoke方法
        helloWorld.sayHello("張三");
    }
}

二、CGLIB動(dòng)態(tài)代理

CGLIB動(dòng)態(tài)代理是通過生成被代理類的子類來增強(qiáng)被代理類功能的方法,因此被代理類必須可以繼承。

1、創(chuàng)建被代理類

package com.teriste.fanshe;
import java.lang.reflect.Method;
public class ReflectServiceImpl {
    public void sayHello(String name){
        System.out.println("Hello "+name);
    }
    public static void main(String[] args) {
        ReflectServiceImpl target = null;
        try {
            target = (ReflectServiceImpl)Class.forName("com.teriste.fanshe.ReflectServiceImpl").newInstance();
            Method method = target.getClass().getMethod("sayHello",String.class);
            method.invoke(target,"張三");
        }catch (NoSuchMethodException | ClassNotFoundException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

2、創(chuàng)建代理類

實(shí)現(xiàn)MethodInterceptor接口

package com.teriste.fanshe;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
 * cglib動(dòng)態(tài)代理
 * 代理類必須可以繼承
 * 無法代理靜態(tài)方法,需要使用asm修改字節(jié)碼的靜態(tài)代理方式
 */
public class CglibProxy implements MethodInterceptor{
    /**
     * 生成CGLIB動(dòng)態(tài)代理對(duì)象
     * @param cls 被代理類的CLASS類
     * @return 類的CGLIB代理對(duì)象
     */
    public Object getProxy(Class cls){
        //CGLIB enhancer 增強(qiáng)類對(duì)象
        Enhancer enhancer = new Enhancer();
        //設(shè)置增強(qiáng)類型
        enhancer.setSuperclass(cls);
        //定義代理邏輯對(duì)象為當(dāng)前對(duì)象,當(dāng)前對(duì)象必須實(shí)現(xiàn)MethodInterceptor.intercept方法
        enhancer.setCallback(this);
        //生成并返回代理對(duì)象
        return enhancer.create();
    }
    /**
     * 代理邏輯方法
     * @param o 代理對(duì)象
     * @param method 被代理類的方法
     * @param objects 方法入?yún)?
     * @param methodProxy 方法代理
     * @return 代理邏輯返回
     * @throws Throwable
     */
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("調(diào)用真實(shí)對(duì)象前");
        methodProxy.invokeSuper(o,objects);
        System.out.println("調(diào)用真實(shí)對(duì)象后");
        return null;
    }
    public static void main(String[] args) {
        CglibProxy cglibProxy = new CglibProxy();
        ReflectServiceImpl reflectService = (ReflectServiceImpl) cglibProxy.getProxy(ReflectServiceImpl.class);
        reflectService.sayHello("張三");
    }
}

三、代理類的靜態(tài)方法

cglib雖然可以代理類的方法,但是對(duì)于類的靜態(tài)方法卻無效,針對(duì)靜態(tài)方法需要使用asm重寫類的字節(jié)碼來實(shí)現(xiàn)代理效果。

到此這篇關(guān)于Java中的動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例的文章就介紹到這了,更多相關(guān)Java動(dòng)態(tài)代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Springboot事務(wù)管理

    詳解Springboot事務(wù)管理

    本篇文章主要介紹了詳解Springboot事務(wù)管理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • mybatis如何使用Java8的日期LocalDate和LocalDateTime詳解

    mybatis如何使用Java8的日期LocalDate和LocalDateTime詳解

    這篇文章主要給大家介紹了關(guān)于mybatis如何使用Java8的日期LocalDate和LocalDateTime的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • Java實(shí)現(xiàn)連連看算法

    Java實(shí)現(xiàn)連連看算法

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)連連看算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 關(guān)于RestTemplate的使用深度解析

    關(guān)于RestTemplate的使用深度解析

    這篇文章主要介紹了對(duì)RestTemplate的深度解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 查看import的類是出自哪個(gè)jar包的方法

    查看import的類是出自哪個(gè)jar包的方法

    下面小編就為大家?guī)硪黄榭磇mport的類是出自哪個(gè)jar包的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • ReentrantReadWriteLock?讀寫鎖分析總結(jié)

    ReentrantReadWriteLock?讀寫鎖分析總結(jié)

    這篇文章主要介紹了ReentrantReadWriteLock 讀寫鎖分析總結(jié),ReentranReadWriteLock中有兩把鎖,一把讀鎖,一把寫鎖,關(guān)于這兩把鎖的介紹,需要的小伙伴可以參考一下
    2022-05-05
  • Spring Boot 使用Druid詳解

    Spring Boot 使用Druid詳解

    本篇文章主要介紹了Spring Boot 使用Druid配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java優(yōu)化重復(fù)冗余代碼的8種方式總結(jié)

    Java優(yōu)化重復(fù)冗余代碼的8種方式總結(jié)

    日常開發(fā)中,我們經(jīng)常會(huì)遇到一些重復(fù)代碼,最近小編優(yōu)化了一些系統(tǒng)中的重復(fù)代碼,用了好幾種的方式,感覺挺有用的,所以本文給大家講講優(yōu)化重復(fù)代碼的幾種方式
    2023-08-08
  • IDEA 每次新建工程都要重新配置 Maven的解決方案

    IDEA 每次新建工程都要重新配置 Maven的解決方案

    這篇文章主要介紹了IDEA 每次新建工程都要重新配置Maven解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • GateWay動(dòng)態(tài)路由與負(fù)載均衡詳細(xì)介紹

    GateWay動(dòng)態(tài)路由與負(fù)載均衡詳細(xì)介紹

    這篇文章主要介紹了GateWay動(dòng)態(tài)路由與負(fù)載均衡,GateWay支持自動(dòng)從注冊(cè)中心中獲取服務(wù)列表并訪問,即所謂的動(dòng)態(tài)路由
    2022-11-11

最新評(píng)論