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

成都动力无限网站推广网站权限设计方案

成都动力无限网站推广,网站权限设计方案,网站建设 排名,wordpress 360 google代码地址 https://github.com/cmdch2017/JDKproxy.git/ 我的理解 我的理解是本身service-serviceImpl结构,新增一个代理对象proxy,代理对象去直接访问serviceImpl,在proxy进行事务的增强操作,所以代理对象实现了接口。如何实现…

代码地址

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.yayakq.cn/news/866048/

相关文章:

  • 自己建网站程序搜索引擎的网址有哪些
  • 彭州建设网站什么叫软件外包公司
  • 做网站下面会有小广告html5video网站
  • 网站查询备案服务商医院网站建设 利法拉网络
  • 建设银行快审额度查询网站教育网站开发价钱
  • 网站建设的教程深圳全网推广平台
  • 庙行网站建设视觉设计专业
  • 网站建设需要哪些工作微信运营者和管理员的区别
  • 网站一个页面多少钱陕西网站建设设计公司
  • 网站开发vs2013怎么查看网站服务器位置
  • 郑州网站建设品牌好腾讯云wordpress 主机
  • 成都市住房与城乡建设厅网站怎样把域名和做的网站连接
  • 郑州网站优化多少钱知名网站制作公司
  • 行业网站建设的书做蛋白go分析网站
  • 网页制作与网站建设宝典pdf长春网站建设方案托管
  • 长春网站制作优势吉网传媒个人博客网站域名注册
  • 通用网站建设如何做网站淘宝客
  • 餐饮网站建设的毕设报告wordpress 游戏 模板下载
  • 饿了么网站做要多少钱php网站开发与维护职位要求
  • 宁夏网站设计在哪里天津建设招标网站首页
  • 百度网盘搜索引擎网站设计制作属于哪个行业
  • 怎么给网站做域名重定向无锡有没有做网站的公司
  • 用网站做数据库吗东莞网站推广裙
  • 金阊公司网站建设电话品牌公关策划案例
  • h5网站开发 源码城市建设和房屋管理部门网站
  • 怒江北京网站建设台州网站建设公司哪个好
  • 微信绑定网站建设网站能赚钱
  • 医院网站前置审批最快多久出来网站建设 石家庄
  • 建材网站的模板南宁百姓网官网
  • 做网站怎么引流网站栏目做树形结构图