Spring框架AOP基礎(chǔ)之代理模式詳解
一、模擬場景
創(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ù)功能時分散了精力
- 附加功能分散在各個業(yè)務(wù)功能方法中,不利于統(tǒng)一維護(hù)
②解決思路
解決這兩個問題,核心就是:解耦。我們需要把附加功能從業(yè)務(wù)功能代碼中抽取出來。
③困難
解決問題的困難:要抽取的代碼在方法內(nèi)部,靠以前把子類中的重復(fù)代碼抽取到父類的方式?jīng)]法解決。 所以需要引入新的技術(shù)。
二、代理模式
靜態(tài)代理
①介紹
二十三種設(shè)計模式中的一種,屬于結(jié)構(gòu)型模式。它的作用就是通過提供一個代理類,讓我們在調(diào)用目標(biāo) 方法的時候,不再是直接對目標(biāo)方法進(jìn)行調(diào)用,而是通過代理類間接調(diào)用。讓不屬于目標(biāo)方法核心邏輯 的代碼從目標(biāo)方法中剝離出來——解耦。調(diào)用目標(biāo)方法時先調(diào)用代理對象的方法,減少對目標(biāo)方法的調(diào) 用和打擾,同時讓附加功能能夠集中在一起也有利于統(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)了解耦,但是由于代碼都寫死了,完全不具備任何的靈活性。就拿日志功能來說,將來其他地方也需要附加日志,那還得再聲明更多個靜態(tài)代理類,那就產(chǎn)生了大量重復(fù)的代碼,日志功能還是分散的,沒有統(tǒng)一管理。
提出進(jìn)一步的需求:將日志功能集中到一個代理類中,將來有任何日志需求,都通過這一個代理類來實(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)文章
springboot X-Accel-Redirect 大文件下載實(shí)現(xiàn)
本文主要介紹了springboot X-Accel-Redirect 大文件下載實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Spring注解配置AOP導(dǎo)致通知執(zhí)行順序紊亂解決方案
這篇文章主要介紹了Spring注解配置AOP導(dǎo)致通知執(zhí)行順序紊亂解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法
Java中可以通過Thread類和Runnable接口來創(chuàng)建多個線程,下面這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法的相關(guān)資料,需要的朋友可以參考下2021-09-09Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離
這篇文章主要介紹了Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離,涉及了部分編程坐標(biāo)的問題,具有一定參考價值,需要的朋友可以了解下2017-09-09基于Java利用static實(shí)現(xiàn)單例模式
這篇文章主要介紹了基于Java利用static實(shí)現(xiàn)單例模式,當(dāng)在多個線程同時觸發(fā)類的初始化過程的時候static不會被多次執(zhí)行,下面我們一起進(jìn)入文章看看具體要的原因2022-01-01