java中動(dòng)態(tài)代理的實(shí)現(xiàn)
動(dòng)態(tài)代理的實(shí)現(xiàn)
使用的模式:代理模式。
代理模式的作用是:為其他對象提供一種代理以控制對這個(gè)對象的訪問。類似租房的中介。
兩種動(dòng)態(tài)代理:
(1)jdk動(dòng)態(tài)代理,jdk動(dòng)態(tài)代理是由Java內(nèi)部的反射機(jī)制來實(shí)現(xiàn)的,目標(biāo)類基于統(tǒng)一的接口(InvocationHandler)
(2)cglib動(dòng)態(tài)代理,cglib動(dòng)態(tài)代理底層則是借助asm來實(shí)現(xiàn)的,cglib這種第三方類庫實(shí)現(xiàn)的動(dòng)態(tài)代理應(yīng)用更加廣泛,且在效率上更有優(yōu)勢。
主要應(yīng)用的框架:
Spring中的AOP,Struts2中的攔截器
具體實(shí)現(xiàn):
1、定義接口和實(shí)現(xiàn)類
package com.example.service; public interface UserService { public String getName(int id); public Integer getAge(int id); }
package com.example.service.impl; import com.example.service.UserService; public class UserServiceImpl implements UserService { public String getName(int id) { System.out.println("------getName------"); return "cat"; } public Integer getAge(int id) { System.out.println("------getAge------"); return 10; } }
2、jdk動(dòng)態(tài)代理實(shí)現(xiàn)
package com.example.jdk; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class MyInvocationHandler implements InvocationHandler { private Object target; /** * 綁定委托對象并返回一個(gè)代理類 * * @param target * @return */ public Object bind(Object target) { this.target = target; //取得代理對象 return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); //要綁定接口(這是一個(gè)缺陷,cglib彌補(bǔ)了這一缺陷) } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ("getName".equals(method.getName())) { System.out.println("------before " + method.getName() + "------"); Object result = method.invoke(target, args); System.out.println("------after " + method.getName() + "------"); return result; } else { Object result = method.invoke(target, args); return result; } } }
package com.example.jdk; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; /** * 測試類 */ public class RunJDK { public static void main(String[] args) { MyInvocationHandler proxy = new MyInvocationHandler(); UserService userServiceProxy = (UserService) proxy.bind(new UserServiceImpl()); System.out.println(userServiceProxy.getName(1)); System.out.println(userServiceProxy.getAge(1)); } }
運(yùn)行結(jié)果:
------before getName------
------getName------
------after getName------
cat
------getAge------
10
3、cglib動(dòng)態(tài)代理實(shí)現(xiàn):
JDK的動(dòng)態(tài)代理機(jī)制只能代理實(shí)現(xiàn)了接口的類,而不能實(shí)現(xiàn)接口的類就不能實(shí)現(xiàn)JDK的動(dòng)態(tài)代理,cglib是針對類來實(shí)現(xiàn)代理的,他的原理是對指定的目標(biāo)類生成一個(gè)子類,并覆蓋其中方法實(shí)現(xiàn)增強(qiáng),但因?yàn)椴捎玫氖抢^承,所以不能對final修飾的類進(jìn)行代理。
CGLIB的核心類:
net.sf.cglib.proxy.Enhancer – 主要的增強(qiáng)類
net.sf.cglib.proxy.MethodInterceptor – 主要的方法攔截類,它是Callback接口的子接口,需要用戶實(shí)現(xiàn)
net.sf.cglib.proxy.MethodProxy – JDK的java.lang.reflect.Method類的代理類,可以方便的實(shí)現(xiàn)對源對象方法的調(diào)用。
net.sf.cglib.proxy.MethodInterceptor接口是最通用的回調(diào)(callback)類型,它經(jīng)常被基于代理的AOP用來實(shí)現(xiàn)攔截(intercept)方法的調(diào)用。這個(gè)接口只定義了一個(gè)方法
public Object intercept(Object object, java.lang.reflect.Method method,
Object[] args, MethodProxy proxy) throws Throwable;
第一個(gè)參數(shù)是代理對像,第二和第三個(gè)參數(shù)分別是攔截的方法和方法的參數(shù)。原來的方法可能通過使用java.lang.reflect.Method對象的一般反射調(diào)用,或者使用 net.sf.cglib.proxy.MethodProxy對象調(diào)用。net.sf.cglib.proxy.MethodProxy通常被首選使用,因?yàn)樗臁?/p>
package com.example.cglib; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class CGLIBProxy implements MethodInterceptor { private Object target; /** * 創(chuàng)建代理對象 * * @param target * @return */ public Object getInstance(Object target) { this.target = target; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(this.target.getClass()); // 回調(diào)方法 enhancer.setCallback(this); // 創(chuàng)建代理對象 return enhancer.create(); } @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("++++++before " + methodProxy.getSuperName() + "++++++"); System.out.println(method.getName()); Object result = methodProxy.invokeSuper(o, objects); System.out.println("++++++after " + methodProxy.getSuperName() + "++++++"); return result; } }
package com.example.cglib; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; /** * 測試CGLIB */ public class RunCGLIB { public static void main(String[] args) { CGLIBProxy cglibProxy = new CGLIBProxy(); UserService userService = (UserService) cglibProxy.getInstance(new UserServiceImpl()); userService.getName(1); userService.getAge(1); } }
運(yùn)行結(jié)果:
++++++before CGLIB$getName$0++++++
getName
------getName------
++++++after CGLIB$getName$0++++++
++++++before CGLIB$getAge$1++++++
getAge
------getAge------
++++++after CGLIB$getAge$1++++++
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
在SpringBoot中使用YourKit進(jìn)行性能調(diào)優(yōu)的教程詳解
在應(yīng)用程序的開發(fā)過程中,性能調(diào)優(yōu)是一個(gè)重要的環(huán)節(jié),在SpringBoot應(yīng)用程序中,我們可以使用YourKit來進(jìn)行性能調(diào)優(yōu),YourKit是一款非常強(qiáng)大的Java性能調(diào)優(yōu)工具,在本文中,我們將介紹如何在 SpringBoot應(yīng)用程序中使用YourKit進(jìn)行性能調(diào)優(yōu)2023-06-06java程序運(yùn)行時(shí)內(nèi)存分配詳解
這篇文章主要介紹了java程序運(yùn)行時(shí)內(nèi)存分配詳解 ,需要的朋友可以參考下2016-07-07Spring?@Bean?修飾方法時(shí)注入?yún)?shù)的操作方法
對于 Spring 而言,IOC 容器中的 Bean 對象的創(chuàng)建和使用是一大重點(diǎn),Spring 也為我們提供了注解方式創(chuàng)建 bean 對象:使用 @Bean,這篇文章主要介紹了Spring?@Bean?修飾方法時(shí)如何注入?yún)?shù),需要的朋友可以參考下2023-10-10關(guān)于Springboot+gateway整合依賴并處理依賴沖突問題
這篇文章主要介紹了Springboot+gateway整合依賴并處理依賴沖突問題,給大家提到了spring boot版本和spring cloud版本,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Java 生成隨機(jī)單據(jù)號的實(shí)現(xiàn)示例
本文主要介紹了Java 生成隨機(jī)單據(jù)號的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09IDEA 重新導(dǎo)入依賴maven 命令 reimport的方法
這篇文章主要介紹了IDEA 重新導(dǎo)入依賴maven 命令 reimport的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Java利用Phantomjs實(shí)現(xiàn)生成圖片的功能
這篇文章主要介紹了Java利用Phantomjs實(shí)現(xiàn)生成圖片的功能,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08MyBatis-plus數(shù)據(jù)庫字段排序不準(zhǔn)確的解決
這篇文章主要介紹了MyBatis-plus數(shù)據(jù)庫字段排序不準(zhǔn)確的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02