当前位置:网站首页>Cglib代理-代码增强测试
Cglib代理-代码增强测试
2022-07-02 06:09:00 【一眉程序猿】
导包
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
编写方法拦截器
- 方法增强具体逻辑
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
import java.util.Arrays;
/** * 自定义 cglib 代理 * * @author Ara * @since 2022/6/20 */
public class MyCglibProxyMethodInterceptor<T> implements MethodInterceptor {
/** * 目标对象 */
private final T target;
/** * 构造方法 */
public MyCglibProxyMethodInterceptor(T target) {
this.target = target;
}
/** * 获取代理对象 */
public T getProxyObject(){
Enhancer enhancer = new Enhancer();
//设置父类 因为cglib是针对指定的类生成一个子类用于其增强行为
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(this);
return (T) enhancer.create();
}
// TODO 执行增强部分
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("执行前 方法参数: " + Arrays.toString(objects));
Object result = method.invoke(target, objects);
System.out.println("执行后 返回值: " + result);
return result;
}
}
编写待增强测试类
- 增强目标点
/** * 文章 Service * * @author Ara * @since 2022/6/20 */
public class ArticleService {
public String saveArticle(String article) {
System.out.printf("保存文章[%s]...%n", article);
return article;
}
}
编写测试
- 检验是否能够进行增强
public class MainTest {
public static void main(String[] args) {
ArticleService articleService = new ArticleService();
//声明Cglib方法拦截增强实现类
MyCglibProxyMethodInterceptor<ArticleService> cglibProxyMethodInterceptor = new MyCglibProxyMethodInterceptor<>(articleService);
// 获取代理类
ArticleService articleServiceProxy = cglibProxyMethodInterceptor.getProxyObject();
//使用代理类调用方法
articleServiceProxy.saveArticle("Java");
}
}
验证结果

小总结
简单对比cglib与JDK动态代理:
- 这里使用cglib进行代理时,不论其目标类是否实现了接口,都可以对其进行增强。
- 但是单独使用JDK动态代理,就只能增强实现了接口的类重写的方法。
以下是对cglib和JDK动态代理是否能够增强有无接口实现的类的对比代码,感兴趣的读者可以进行测试:
public class MainTest {
public static void main(String[] args) {
cglibProxyNoInterfaceTest();
}
/** * cglib代理(代理目标类未实现接口) */
private static void cglibProxyNoInterfaceTest(){
ArticleService articleService = new ArticleService();
//声明Cglib方法拦截增强实现类
MyCglibProxyMethodInterceptor<ArticleService> cglibProxyMethodInterceptor = new MyCglibProxyMethodInterceptor<>(articleService);
// 获取代理类
ArticleService articleServiceProxy = cglibProxyMethodInterceptor.getProxyObject();
//使用代理类调用方法
articleServiceProxy.saveArticle("Java");
}
/** * cglib代理(代理目标类实现了接口) */
private static void cglibProxyHasInterfaceTest(){
UserService userService = new UserServiceImpl();
//声明Cglib方法拦截增强实现类
MyCglibProxyMethodInterceptor<UserService> cglibProxyMethodInterceptor = new MyCglibProxyMethodInterceptor<>(userService);
// 获取代理类
UserService userServiceProxy = cglibProxyMethodInterceptor.getProxyObject();
//使用代理类调用方法
userServiceProxy.add("Ara");
}
/** * JDK动态代理(代理目标类实现了接口) */
private static void jdkDynamicProxyHasInterfaceTest(){
//原对象 (接口接收参数)
UserService userService = new UserServiceImpl();
//声明增强处理程序
MyJdkProxyInvocationHandler<UserService> proxyInvocationHandler = new MyJdkProxyInvocationHandler<>(userService);
//获取代理对象
UserService proxyUserService = proxyInvocationHandler.getProxyObject();
//使用代理对象来调用方法
proxyUserService.add("Ara");
}
/** * JDK动态代理(代理目标类未实现接口) * 有误,使用JDK动态代理实现增强,必须要求代理目标实现了接口 */
private static void jdkDynamicProxyNoInterfaceTest(){
ArticleService articleService = new ArticleService();
//声明增强处理程序
MyJdkProxyInvocationHandler<ArticleService> proxyInvocationHandler = new MyJdkProxyInvocationHandler<>(articleService);
//获取代理对象
ArticleService proxyArticleService = proxyInvocationHandler.getProxyObject();
//使用代理对象来调用方法
proxyArticleService.saveArticle("Java");
}
}
边栏推荐
- Frequently asked questions about jetpack compose and material you
- Comment utiliser mitmproxy
- LeetCode 40. 组合总和 II
- Problems encountered in uni app development (continuous update)
- LeetCode 39. 组合总和
- LeetCode 77. 组合
- Shenji Bailian 3.53-kruskal
- Monitoring uplink of VRRP
- 深入学习JVM底层(四):类文件结构
- Ti millimeter wave radar learning (I)
猜你喜欢

500. Keyboard line

Spark overview

Deep learning classification network -- vggnet

让每一位开发者皆可使用机器学习技术

CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch

Sumo tutorial Hello World

Linear DP (split)

WLAN相关知识点总结

Eco express micro engine system has supported one click deployment to cloud hosting

It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
随机推荐
Cookie plugin and localforce offline storage plugin
The difference between session and cookies
TI毫米波雷达学习(一)
From design delivery to development, easy and efficient!
【C语言】简单实现扫雷游戏
格式校验js
来自读者们的 I/O 观后感|有奖征集获奖名单
Shenji Bailian 3.52-prim
STC8H8K系列匯編和C51實戰——數碼管顯示ADC、按鍵串口回複按鍵號與ADC數值
Lambda expressions and method references
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
492. Construction rectangle
The real definition of open source software
Verifying downloaded files using sha256 files
Spark overview
Ti millimeter wave radar learning (I)
ROS2----LifecycleNode生命周期节点总结
Let every developer use machine learning technology
加密压缩文件解密技巧
Comment utiliser mitmproxy