最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring框架AOP基礎(chǔ)之代理模式詳解

 更新時(shí)間:2022年11月09日 10:45:45   作者:學(xué)習(xí)使我快樂T  
代理模式(Proxy Parttern)為一個(gè)對象提供一個(gè)替身,來控制這個(gè)對象的訪問,即通過代理對象來訪問目標(biāo)對象。本文將通過示例詳細(xì)講解一下這個(gè)模式,需要的可以參考一下

一、模擬場景

創(chuàng)建接口

public interface Calculator {
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}

創(chuàng)建實(shí)現(xiàn)類

public class CalculatorPureImpl implements Calculator{
    @Override
    public int add(int i, int j) {
        System.out.println("日志,方法:add,參數(shù):" + i + "," + j);
        int result = i + j;
        System.out.println("方法內(nèi)部,result" + result);
        System.out.println("日志,方法:add,結(jié)果:" + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        System.out.println("日志,方法:sub,參數(shù):" + i + "," + j);
        int result = i - j;
        System.out.println("方法內(nèi)部,result" + result);
        System.out.println("日志,方法:sub,結(jié)果:" + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        System.out.println("日志,方法:mul,參數(shù):" + i + "," + j);
        int result = i * j;
        System.out.println("方法內(nèi)部,result" + result);
        System.out.println("日志,方法:mul,結(jié)果:" + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        System.out.println("日志,方法:div,參數(shù):" + i + "," + j);
        int result = i / j;
        System.out.println("方法內(nèi)部,result" + result);
        System.out.println("日志,方法:div,結(jié)果:" + result);
        return result;
    }
}

發(fā)現(xiàn)這些日志信息非常多余

提出問題

①現(xiàn)有代碼缺陷

  • 針對帶日志功能的實(shí)現(xiàn)類,我們發(fā)現(xiàn)有如下缺陷: 對核心業(yè)務(wù)功能有干擾,導(dǎo)致程序員在開發(fā)核心業(yè)務(wù)功能時(shí)分散了精力
  • 附加功能分散在各個(gè)業(yè)務(wù)功能方法中,不利于統(tǒng)一維護(hù)

②解決思路

解決這兩個(gè)問題,核心就是:解耦。我們需要把附加功能從業(yè)務(wù)功能代碼中抽取出來。

③困難

解決問題的困難:要抽取的代碼在方法內(nèi)部,靠以前把子類中的重復(fù)代碼抽取到父類的方式?jīng)]法解決。 所以需要引入新的技術(shù)。

二、代理模式

靜態(tài)代理

①介紹

二十三種設(shè)計(jì)模式中的一種,屬于結(jié)構(gòu)型模式。它的作用就是通過提供一個(gè)代理類,讓我們在調(diào)用目標(biāo) 方法的時(shí)候,不再是直接對目標(biāo)方法進(jìn)行調(diào)用,而是通過代理類間接調(diào)用。讓不屬于目標(biāo)方法核心邏輯 的代碼從目標(biāo)方法中剝離出來——解耦。調(diào)用目標(biāo)方法時(shí)先調(diào)用代理對象的方法,減少對目標(biāo)方法的調(diào) 用和打擾,同時(shí)讓附加功能能夠集中在一起也有利于統(tǒng)一維護(hù)。

接口:

public interface Calculator {
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}

核心實(shí)現(xiàn)類:

package com.tian.spring.proxy;
public class CalculatorImpl implements Calculator{
    @Override
    public int add(int i, int j) {
        int result = i + j;
        System.out.println("方法內(nèi)部,result:" + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        int result = i - j;
        System.out.println("方法內(nèi)部,result:" + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        int result = i * j;
        System.out.println("方法內(nèi)部,result:" + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        int result = i / j;
        System.out.println("方法內(nèi)部,result:" + result);
        return result;
    }
}

代理類:

public class CalculatorStaticProxy implements Calculator{
    private CalculatorImpl target;
    public CalculatorStaticProxy(CalculatorImpl target) {
        this.target = target;
    }
    @Override
    public int add(int i, int j) {
        System.out.println("日志,方法:add,參數(shù):" + i + "," + j);
        int result = target.add(i, j);
        System.out.println("日志,方法:add,結(jié)果:" + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        System.out.println("日志,方法:add,參數(shù):" + i + "," + j);
        int result = target.sub(i, j);
        System.out.println("日志,方法:add,結(jié)果:" + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        System.out.println("日志,方法:add,參數(shù):" + i + "," + j);
        int result = target.mul(i, j);
        System.out.println("日志,方法:add,結(jié)果:" + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        System.out.println("日志,方法:add,參數(shù):" + i + "," + j);
        int result = target.div(i, j);
        System.out.println("日志,方法:add,結(jié)果:" + result);
        return result;
    }
}

測試類:

public class ProxyTest {
    @Test
    public void testProxy() {
        CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorImpl());
        proxy.add(1,4);
    }

靜態(tài)代理確實(shí)實(shí)現(xiàn)了解耦,但是由于代碼都寫死了,完全不具備任何的靈活性。就拿日志功能來說,將來其他地方也需要附加日志,那還得再聲明更多個(gè)靜態(tài)代理類,那就產(chǎn)生了大量重復(fù)的代碼,日志功能還是分散的,沒有統(tǒng)一管理。

提出進(jìn)一步的需求:將日志功能集中到一個(gè)代理類中,將來有任何日志需求,都通過這一個(gè)代理類來實(shí)現(xiàn)。這就需要使用動態(tài)代理技術(shù)了。

動態(tài)代理

動態(tài)代理有兩種:

1.jdk動態(tài)代理,要求必須有接口,最終生成的代理類和目標(biāo)類實(shí)現(xiàn)相同的接口 在com.sun.proxy包下,類名為$proxy2

2.cglib動態(tài)代理,最終生成的代理類會繼承目標(biāo)類,并且和目標(biāo)類在相同的包下

①創(chuàng)建工廠類

public class ProxyFactory {
    private Object target;
    public ProxyFactory(Object target) {
        this.target = target;
    }
    public Object getProxy() {
        /**
         * ClassLoader loader:指定加載動態(tài)生成的代理類的類加載器
         * Class[] interfaces:獲取目標(biāo)對象實(shí)現(xiàn)的所有接口的class對象的數(shù)組
         * InvocationHandler h:設(shè)置代理類中的抽象方法如何重寫
         */
        ClassLoader classLoader = this.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        InvocationHandler h = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object result = null;
                try {
                    System.out.println("日志,方法:" + method.getName() + ",參數(shù):" + Arrays.toString(args));
                    //proxy表示代理對象,method表示要執(zhí)行的方法,args表示要執(zhí)行的方法的參數(shù)列表
                    result = method.invoke(target, args);
                    System.out.println("日志,方法:" + method.getName() + ",結(jié)果:" + result);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("日志,方法:" + method.getName() + ",異常:" + e);
                } finally {
                    System.out.println("日志,方法:" + method.getName() + ",方法執(zhí)行完畢:");
                }
                return result;
            }
        };
        return Proxy.newProxyInstance(classLoader,interfaces,h);
    }
}

②測試類

public class ProxyTest {
    @Test
    public void testProxy() {
//        CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorImpl());
//        proxy.add(1,4);
        ProxyFactory proxyFactory = new ProxyFactory(new CalculatorImpl());
        Calculator proxy = (Calculator) proxyFactory.getProxy();
        proxy.div(1,0);
    }
}

到此這篇關(guān)于Spring框架AOP基礎(chǔ)之代理模式詳解的文章就介紹到這了,更多相關(guān)Spring代理模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解JAVA動態(tài)代理

    詳解JAVA動態(tài)代理

    這篇文章主要介紹了JAVA動態(tài)代理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • org.springframework.web.client.ResourceAccessException資源訪問錯(cuò)誤的解決方法

    org.springframework.web.client.ResourceAccessException資源訪問錯(cuò)誤

    本文主要介紹了org.springframework.web.client.ResourceAccessException資源訪問錯(cuò)誤的解決方法,首先需要分析異常的詳細(xì)信息,以確定具體的錯(cuò)誤原因,感興趣的可以了解一下
    2024-05-05
  • SpringBoot使用EmbeddedDatabaseBuilder進(jìn)行數(shù)據(jù)庫集成測試

    SpringBoot使用EmbeddedDatabaseBuilder進(jìn)行數(shù)據(jù)庫集成測試

    在開發(fā)SpringBoot應(yīng)用程序時(shí),我們通常需要與數(shù)據(jù)庫進(jìn)行交互,為了確保我們的應(yīng)用程序在生產(chǎn)環(huán)境中可以正常工作,我們需要進(jìn)行數(shù)據(jù)庫集成測試,在本文中,我們將介紹如何使用 SpringBoot 中的 EmbeddedDatabaseBuilder 來進(jìn)行數(shù)據(jù)庫集成測試
    2023-07-07
  • 使用Maven將springboot工程打包成docker鏡像

    使用Maven將springboot工程打包成docker鏡像

    這篇文章主要介紹了使用Maven將springboot工程打包成docker鏡像,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 用java實(shí)現(xiàn)跳動的小球示例代碼

    用java實(shí)現(xiàn)跳動的小球示例代碼

    這篇文章主要介紹了用java實(shí)現(xiàn)跳動的小球,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • springboot3?redis?常用操作工具類詳解

    springboot3?redis?常用操作工具類詳解

    本文詳細(xì)介紹了Spring Boot 3中使用Spring Data Redis進(jìn)行Redis操作的工具類實(shí)現(xiàn),該工具類涵蓋了字符串、哈希、列表、集合和有序集合等常用功能,感興趣的朋友一起看看吧
    2025-01-01
  • 如何使用JaCoCo分析java單元測試覆蓋率

    如何使用JaCoCo分析java單元測試覆蓋率

    在做單元測試時(shí),代碼覆蓋率常常被拿來作為衡量測試好壞的指標(biāo),甚至,用代碼覆蓋率來考核測試任務(wù)完成情況,比如,代碼覆蓋率必須達(dá)到80%或 90%。于是乎,測試人員費(fèi)盡心思設(shè)計(jì)案例覆蓋代碼。下面我們來學(xué)習(xí)一下吧
    2019-06-06
  • java兩個(gè)線程同時(shí)寫一個(gè)文件

    java兩個(gè)線程同時(shí)寫一個(gè)文件

    這篇文章主要為大家詳細(xì)介紹了java兩個(gè)線程同時(shí)寫一個(gè)文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 簡述Java異步上傳文件的三種方式

    簡述Java異步上傳文件的三種方式

    這篇文章主要為大家詳細(xì)介紹了Java異步上傳文件的三種方式,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Java阻塞隊(duì)列的實(shí)現(xiàn)及應(yīng)用

    Java阻塞隊(duì)列的實(shí)現(xiàn)及應(yīng)用

    這篇文章主要介紹了剖析Java中阻塞隊(duì)列的實(shí)現(xiàn)原理及應(yīng)用場景,這里也對阻塞和非阻塞隊(duì)列的不同之處進(jìn)行了對比,需要的朋友可以參考下
    2021-10-10

最新評論

宁晋县| 北安市| 社会| 营口市| 沈丘县| 黄平县| 林西县| 浦城县| 乐昌市| 孙吴县| 禄丰县| 昭平县| 浮梁县| 邢台县| 忻城县| 承德县| 麦盖提县| 呈贡县| 北流市| 和政县| 肇东市| 库伦旗| 大姚县| 土默特左旗| 平江县| 祥云县| 扶绥县| 琼海市| 汤原县| 吴忠市| 平顶山市| 贵港市| 百色市| 鹤峰县| 泸水县| 阿图什市| 崇礼县| 棋牌| 丘北县| 栾城县| 廊坊市|