java 動(dòng)態(tài)代理的方法總結(jié)
java 動(dòng)態(tài)代理的方法總結(jié)
AOP的攔截功能是由java中的動(dòng)態(tài)代理來實(shí)現(xiàn)的。說白了,就是在目標(biāo)類的基礎(chǔ)上增加切面邏輯,生成增強(qiáng)的目標(biāo)類(該切面邏輯或者在目標(biāo)類函數(shù)執(zhí)行之前,或者目標(biāo)類函數(shù)執(zhí)行之后,或者在目標(biāo)類函數(shù)拋出異常時(shí)候執(zhí)行。不同的切入時(shí)機(jī)對(duì)應(yīng)不同的Interceptor的種類,如BeforeAdviseInterceptor,AfterAdviseInterceptor以及ThrowsAdviseInterceptor等)。
那么動(dòng)態(tài)代理是如何實(shí)現(xiàn)將切面邏輯(advise)織入到目標(biāo)類方法中去的呢?下面我們就來詳細(xì)介紹并實(shí)現(xiàn)AOP中用到的兩種動(dòng)態(tài)代理。
AOP的源碼中用到了兩種動(dòng)態(tài)代理來實(shí)現(xiàn)攔截切入功能:jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理。兩種方法同時(shí)存在,各有優(yōu)劣。jdk動(dòng)態(tài)代理是由Java內(nèi)部的反射機(jī)制來實(shí)現(xiàn)的,cglib動(dòng)態(tài)代理底層則是借助asm來實(shí)現(xiàn)的??偟膩碚f,反射機(jī)制在生成類的過程中比較高效,而asm在生成類之后的相關(guān)執(zhí)行過程中比較高效(可以通過將asm生成的類進(jìn)行緩存,這樣解決asm生成類過程低效問題)。還有一點(diǎn)必須注意:jdk動(dòng)態(tài)代理的應(yīng)用前提,必須是目標(biāo)類基于統(tǒng)一的接口。如果沒有上述前提,jdk動(dòng)態(tài)代理不能應(yīng)用。由此可以看出,jdk動(dòng)態(tài)代理有一定的局限性,cglib這種第三方類庫實(shí)現(xiàn)的動(dòng)態(tài)代理應(yīng)用更加廣泛,且在效率上更有優(yōu)勢。。
1、定義接口和實(shí)現(xiàn)
package com.meituan.hyt.test3.service;
public interface UserService {
public String getName(int id);
public Integer getAge(int id);
}
package com.meituan.hyt.test3.service.impl;
import com.meituan.hyt.test3.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public String getName(int id) {
System.out.println("------getName------");
return "Tom";
}
@Override
public Integer getAge(int id) {
System.out.println("------getAge------");
return 10;
}
}
2、jdk動(dòng)態(tài)代理實(shí)現(xiàn)
package com.meituan.hyt.test3.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler {
private Object target;
MyInvocationHandler() {
super();
}
MyInvocationHandler(Object target) {
super();
this.target = target;
}
@Override
public Object invoke(Object o, 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.meituan.hyt.test3.jdk;
import com.meituan.hyt.test3.service.UserService;
import com.meituan.hyt.test3.service.impl.UserServiceImpl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Main1 {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
InvocationHandler invocationHandler = new MyInvocationHandler(userService);
UserService userServiceProxy = (UserService)Proxy.newProxyInstance(userService.getClass().getClassLoader(),
userService.getClass().getInterfaces(), invocationHandler);
System.out.println(userServiceProxy.getName(1));
System.out.println(userServiceProxy.getAge(1));
}
}
運(yùn)行結(jié)果
++++++before getName++++++ ------getName------ ++++++after getName++++++ Tom ------getAge------ 10
3、cglib動(dòng)態(tài)代理實(shí)現(xiàn)
Cglib是一個(gè)優(yōu)秀的動(dòng)態(tài)代理框架,它的底層使用ASM在內(nèi)存中動(dòng)態(tài)的生成被代理類的子類,使用CGLIB即使代理類沒有實(shí)現(xiàn)任何接口也可以實(shí)現(xiàn)動(dòng)態(tài)代理功能。CGLIB具有簡單易用,它的運(yùn)行速度要遠(yuǎn)遠(yuǎn)快于JDK的Proxy動(dòng)態(tài)代理:
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)對(duì)源對(duì)象方法的調(diào)用,如使用: Object o = methodProxy.invokeSuper(proxy, args);//雖然第一個(gè)參數(shù)是被代理對(duì)象,也不會(huì)出現(xiàn)死循環(huán)的問題。 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ù)是代理對(duì)像,第二和第三個(gè)參數(shù)分別是攔截的方法和方法的參數(shù)。原來的方法可能通過使用java.lang.reflect.Method對(duì)象的一般反射調(diào)用,或者使用 net.sf.cglib.proxy.MethodProxy對(duì)象調(diào)用。net.sf.cglib.proxy.MethodProxy通常被首選使用,因?yàn)樗臁?/p>
package com.meituan.hyt.test3.cglib;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class CglibProxy implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
System.out.println(method.getName());
Object o1 = methodProxy.invokeSuper(o, args);
System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
return o1;
}
}
package com.meituan.hyt.test3.cglib;
import com.meituan.hyt.test3.service.UserService;
import com.meituan.hyt.test3.service.impl.UserServiceImpl;
import net.sf.cglib.proxy.Enhancer;
public class Main2 {
public static void main(String[] args) {
CglibProxy cglibProxy = new CglibProxy();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserServiceImpl.class);
enhancer.setCallback(cglibProxy);
UserService o = (UserService)enhancer.create();
o.getName(1);
o.getAge(1);
}
}
運(yùn)行結(jié)果:
++++++before CGLIB$getName$0++++++ getName ------getName------ ++++++before CGLIB$getName$0++++++ ++++++before CGLIB$getAge$1++++++ getAge ------getAge------ ++++++before CGLIB$getAge$1++++++
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 詳解java JDK 動(dòng)態(tài)代理類分析(java.lang.reflect.Proxy)
- java 中動(dòng)態(tài)代理詳解及實(shí)例
- Java動(dòng)態(tài)代理分析及理解
- java 中動(dòng)態(tài)代理(JDK,cglib)實(shí)例代碼
- 深度剖析java中JDK動(dòng)態(tài)代理機(jī)制
- Java 動(dòng)態(tài)代理深入理解
- JAVA動(dòng)態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)
- java 代理模式及動(dòng)態(tài)代理機(jī)制深入分析
- Java 動(dòng)態(tài)代理與CGLIB詳細(xì)介紹
- Java靜態(tài)代理和動(dòng)態(tài)代理總結(jié)
- 深入理解java動(dòng)態(tài)代理機(jī)制
- Java動(dòng)態(tài)代理分析及簡單實(shí)例
- java 1.8 動(dòng)態(tài)代理源碼深度分析
相關(guān)文章
Java字符串駝峰與下?lián)Q線格式轉(zhuǎn)換如何實(shí)現(xiàn)
這篇文章主要介紹了Java字符串駝峰與下?lián)Q線格式轉(zhuǎn)換如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Java操作Mongodb數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)的增刪查改功能示例
這篇文章主要介紹了Java操作Mongodb數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)的增刪查改功能,結(jié)合完整實(shí)例形式分析了java針對(duì)MongoDB數(shù)據(jù)庫的連接、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Java獲取接口的所有實(shí)現(xiàn)類方法總結(jié)示例
這篇文章主要給大家介紹了關(guān)于Java獲取接口的所有實(shí)現(xiàn)類方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06
SpringBoot中MybatisX插件的簡單使用教程(圖文)
MybatisX 是一款基于 IDEA 的快速開發(fā)插件,方便在使用mybatis以及mybatis-plus開始時(shí)簡化繁瑣的重復(fù)操作,本文主要介紹了SpringBoot中MybatisX插件的簡單使用教程,感興趣的可以了解一下2023-06-06
Java使用easyExcel批量導(dǎo)入數(shù)據(jù)詳解
這篇文章主要介紹了Java使用easyExcel批量導(dǎo)入數(shù)據(jù)詳解,通常我們會(huì)提供一個(gè)模板,此模塊我們可以使用easyExcel導(dǎo)出數(shù)據(jù)生成的一個(gè)Excel文件當(dāng)作模板,提供下載鏈接,用戶在該文件內(nèi)填入規(guī)定的數(shù)據(jù)格式以后可以批量導(dǎo)入數(shù)據(jù)到數(shù)據(jù)庫中,需要的朋友可以參考下2023-08-08

