当前位置: 首页 > news >正文

【笔记+代码】JDK动态代理理解

代码地址

https://github.com/cmdch2017/JDKproxy.git/

我的理解

我的理解是本身service-serviceImpl结构,新增一个代理对象proxy,代理对象去直接访问serviceImpl,在proxy进行事务的增强操作,所以代理对象实现了接口。如何实现动态呢?需要实现InovacationHandler接口,并用反射调用invoke方法,实现类似于泛型一样的效果。

CHATGPT回答

“JDK动态代理是通过Proxy类和InvocationHandler接口实现的。它允许在运行时生成代理类,无需事先定义代理类,从而在不修改原有代码的情况下对方法进行增强。通过实现InvocationHandler接口,我们可以在目标方法执行前后插入自定义逻辑,比如事务处理。动态代理的优势在于避免了手动创建大量代理类的繁琐工作,使代码更加简洁和易维护。”

核心代码

客户端

public class TestStudent {public static void main(String[] args) {
//        testQuery(1);testQueryObject(1);}
//这里是动态代理,多实现了一个InvocationHandlerprivate static void testQueryObject(int id) {DaoTransaction transaction=new DaoTransaction();StudentServiceImpl studentService=new StudentServiceImpl();TransactionHandler transactionHandler=new TransactionHandler(studentService,transaction);StudentService proxyInstance=(StudentService)Proxy.newProxyInstance(StudentServiceImpl.class.getClassLoader(),StudentServiceImpl.class.getInterfaces(),transactionHandler);Student student=proxyInstance.query(id);System.out.println("id:"+student.getId()+",name:"+student.getName());}
//这里是静态代理private static void testQuery(int id) {DaoTransaction transaction=new DaoTransaction();StudentServiceImpl studentService=new StudentServiceImpl();ProxyStudent proxyStudent=new ProxyStudent(studentService,transaction);Student student=proxyStudent.query(id);System.out.println("id:"+student.getId()+",name:"+student.getName());}
}

动态代理学生

public class TransactionHandler implements InvocationHandler {private DaoTransaction daoTransaction;private Object object;public TransactionHandler(Object object, DaoTransaction daoTransaction) {this.object = object;this.daoTransaction = daoTransaction;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {return method.invoke(object,args);}
}
@Data
public class Student {private int id;private String name;
}

静态代理学生

public class ProxyStudent implements StudentService {private StudentServiceImpl studentService;private DaoTransaction daoTransaction;public ProxyStudent(StudentServiceImpl studentService, DaoTransaction daoTransaction) {this.studentService = studentService;this.daoTransaction = daoTransaction;}@Overridepublic Student query(int id) {daoTransaction.startTransaction();Student student=studentService.query(id);daoTransaction.endTransaction();return student;}
}
public class StudentServiceImpl implements StudentService {@Overridepublic Student query(int id) {System.out.println("执行查询");Student student=new Student();student.setId(id);student.setName("lst");return student;}
}
public interface StudentService {Student query(int id);
}
public class DaoTransaction {public void startTransaction() {System.out.println("开启事务");}public void endTransaction() {System.out.println("关闭事务");}
}
http://www.lryc.cn/news/248795.html

相关文章:

  • Java八股文面试全套真题【含答案】-Vue篇
  • 介绍比特币上的 sCrypt 开发平台
  • 什么是路由抖动?该如何控制
  • 2023SICTF-web-白猫-RCE
  • 1.用数组输出0-9
  • Selenium 元素不能定位总结
  • 1-2 非阻塞延时实现LED闪烁功能(累计定时中断次数)--多路软件定时器的功能实现
  • 数据类型及强制转换
  • Python----高阶函数
  • Unity地面交互效果——6、地形动态顶点置换和曲面细分
  • Linux系统服务之一次性服务(2)
  • Vue项目解决van-calendar 显示白色空白,需滑动一下屏幕,才可正常显示
  • Linux:可视化管理工具Webmin的安装
  • WARNING: Access control is not enabled for the database.
  • JavaScript编程进阶 – Return语句
  • Python与设计模式--备忘录模式
  • 04_Flutter自定义Slider滑块
  • 服务器数据恢复—EMC存储raid5故障导致上层应用崩溃的数据恢复案例
  • 7.1 Windows驱动开发:内核监控进程与线程回调
  • 基于ssm的汽车论坛管理系统设计与实现
  • 实习开发日志经验总结(一)
  • 【Unity基础】8.简单场景的搭建
  • 傅里叶变换及其在机器学习中的应用
  • xorm源码学习
  • Vue3中的<script setup>和<script>的区别
  • Docker笔记-Docker搭建最新版zabbix服务端(2023-07-31)
  • QT配合CSS隐藏按钮
  • 2023亚太地区数学建模C题思路分析+模型+代码+论文
  • Linguistic Steganalysis in Few-Shot Scenario论文阅读笔记
  • 详细学习Pyqt5的4种项目部件(Item Widget)