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

网站开发可行性html5网站提示

网站开发可行性,html5网站提示,安卓应用市场app下载安装,网站建设飠金手指排名十五代码地址 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/965923/

相关文章:

  • 专业做网站服务国内免费可商用图片素材网站
  • 深圳做网站需要多少钱整站快速排名
  • 梅州兴宁网站建设培训建站公司主要做那些业务
  • 公司制作网站费用备案多个网站
  • 全能网站建设教程wordpress 手机发文章
  • 自微网站为女朋友做的表白网站
  • 建设银行无锡分行网站内江网站制作
  • 沈阳网站建设 成创商标logo一键生成器
  • 有哪些好的响应式网站有哪些网站分析seo情况
  • 帝国建站软件织梦做的网站如何修改
  • 中企动力网站报价网站建设留言板
  • 做网站用什么软件?网站建设 印花税
  • 怎样做网站卖东西贵州住建局和城乡建设官网
  • 长安外贸网站建设公司上海网站建设 虹口
  • 怎么做付费网站做网站不推广有效果吗
  • 京网站制作公司网页设计需要学什么知识
  • 运城门户网站建设怎么做自动发卡的网站
  • 游戏网站wordpress优秀网页设计作品分析ppt
  • 南通网站定制网站去哪里备案
  • 东莞市电商网站建设优秀企业网站模板
  • 做我女朋网站源码360收录批量查询
  • 图片瀑布流网站模板酒店如何做好线上营销
  • 把自己的电脑做网站服务器山东城市建设职业学院教务网网站
  • 企业网站官网yy直播间
  • 济南手机端建站模板100深夜看黄禁用免费
  • 温州网站建设结构医疗网站建设 中企动力
  • 公司做竞拍网站的收入怎么报税双语网站建设方案
  • html文档模板做网站优化的公司的宣传海报
  • 用帝国做网站好做吗流量推广平台有哪些
  • 做网站需要提供什么资料网站配色 蓝绿