java實(shí)現(xiàn)動態(tài)代理方法淺析
一些Java項(xiàng)目中在mybatis與spring整合中有MapperScannerConfigurer的使用,該類通過反向代理自動生成基于接口的動態(tài)代理類。
有鑒于此,本文淺析了java的動態(tài)代理。
本文使用動態(tài)代理模擬處理事務(wù)的攔截器。
接口:
public interface UserService {
public void addUser();
public void removeUser();
public void searchUser();
}
實(shí)現(xiàn)類:
public class UserServiceImpl implements UserService {
public void addUser() {
System.out.println("add user");
}
public void removeUser() {
System.out.println("remove user");
}
public void searchUser() {
System.out.println("search user");
}
}
java動態(tài)代理的實(shí)現(xiàn)有2種方式
1.jdk自帶的動態(tài)代理
使用jdk自帶的動態(tài)代理需要了解InvocationHandler接口和Proxy類,他們都是在java.lang.reflect包下。
InvocationHandler介紹:
InvocationHandler是代理實(shí)例的調(diào)用處理程序?qū)崿F(xiàn)的接口。
每個代理實(shí)例都具有一個關(guān)聯(lián)的InvocationHandler。對代理實(shí)例調(diào)用方法時,這個方法會調(diào)用InvocationHandler的invoke方法。
Proxy介紹:
Proxy 提供靜態(tài)方法用于創(chuàng)建動態(tài)代理類和實(shí)例。
實(shí)例(模擬AOP處理事務(wù)):
public class TransactionInterceptor implements InvocationHandler {
private Object target;
public void setTarget(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("start Transaction");
method.invoke(target, args);
System.out.println("end Transaction");
return null;
}
}
測試代碼:
public class TestDynamicProxy {
@Test
public void testJDK() {
TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
UserService userService = new UserServiceImpl();
transactionInterceptor.setTarget(userService);
UserService userServiceProxy =
(UserService) Proxy.newProxyInstance(
userService.getClass().getClassLoader(),
userService.getClass().getInterfaces(),
transactionInterceptor);
userServiceProxy.addUser();
}
}
測試結(jié)果:
start Transaction add user end Transaction
很明顯,我們通過userServiceProxy這個代理類進(jìn)行方法調(diào)用的時候,會在方法調(diào)用前后進(jìn)行事務(wù)的開啟和關(guān)閉。
2. 第三方庫cglib
CGLIB是一個功能強(qiáng)大的,高性能、高質(zhì)量的代碼生成庫,用于在運(yùn)行期擴(kuò)展Java類和實(shí)現(xiàn)Java接口。
它與JDK的動態(tài)代理的之間最大的區(qū)別就是:
JDK動態(tài)代理是針對接口的,而cglib是針對類來實(shí)現(xiàn)代理的,cglib的原理是對指定的目標(biāo)類生成一個子類,并覆蓋其中方法實(shí)現(xiàn)增強(qiáng),但因?yàn)椴捎玫氖抢^承,所以不能對final修飾的類進(jìn)行代理。
實(shí)例代碼如下:
public class UserServiceCallBack implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("start Transaction by cglib");
methodProxy.invokeSuper(o, args);
System.out.println("end Transaction by cglib");
return null;
}
}
測試代碼:
public class TestDynamicProxy {
@Test
public void testCGLIB() {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserServiceImpl.class);
enhancer.setCallback(new UserServiceCallBack());
UserServiceImpl proxy = (UserServiceImpl)enhancer.create();
proxy.addUser();
}
}
測試結(jié)果:
start Transaction by cglib add user end Transaction by cglib
感興趣的讀者可以實(shí)際測試一下本文實(shí)例,相信會有很大的收獲。
相關(guān)文章
springboot中PostMapping正常接收json參數(shù)后返回404問題
這篇文章主要介紹了springboot中PostMapping正常接收json參數(shù)后返回404問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
Java實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語句的工具類
這篇文章主要為大家詳細(xì)介紹了如何利用Java編寫一個工具類,可以實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語句,感興趣的小伙伴可以了解一下2023-05-05

