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

robots.txt 禁止爬行整个网站网页模板网站

robots.txt 禁止爬行整个网站,网页模板网站,辛集外贸网站建设,建材行业网站建设方案模拟 Spring 创建的动态代理类 本文主要目的是从父类和子类继承的角度去分析为什么在 Service 标注的业务类中使用 this 调用方法会造成事务失效。解释在这种情况下 this 为什么是原始类对象而不是代理类对象。 问题描述 在 Service 标注的业务类中,如果调用本类…

模拟 Spring 创建的动态代理类

本文主要目的是从父类和子类继承的角度去分析为什么在 @Service 标注的业务类中使用 this 调用方法会造成事务失效。解释在这种情况下 this 为什么是原始类对象而不是代理类对象。

问题描述

在 @Service 标注的业务类中,如果调用本类中的方法,那么会造成事务失效。原因是因为事务的功能是 @Transactional 注解通过 AOP 切面的方式对原始类进行的增强,因此事务功能是代理类对象中的方法才具备的。

现在问题来了,在 CGLib 的动态代理模式中,代理类(假设为 UserServiceImplProxy)是继承了 UserServiceImpl,也就是说代理类是原始类的子类,而通过 Spring 容器的 getBean 方法获取到的也是代理类对象,那么在主方法中调用 userServiceImplProxy.transactionFailTest() 方法,那问题似乎变成了在父类中使用 this 关键字时,this 代表的是子类对象还是父类对象?

先说结论,this 代表的对象是不确定的。

@Service
public class UserServiceImpl {@Autowiredprivate UserMapper userMapper;@Autowiredprivate UserServiceImpl userServiceImpl;public void transactionFailTest() {System.out.println("this=" + this);System.out.println("this.getClass()=" + this.getClass());System.out.println("this.getClass().getSuperclass() = " + this.getClass().getSuperclass());// 重点是探究this对象到底是什么?为什么this不是代理类对象this.transactionTest();}public void transactionSuccessTest() {// 调用代理类中的方法userServiceImpl.transactionTest();}@Transactionalpublic void transactionTest() {userMapper.updatePasswordById(1L, "111111");// if (true) {//     throw new RuntimeException("故意制造异常");// }userMapper.updatePasswordById(2L, "222222");}
}

继承关系中的方法调用

在下面的测试案例中,同样是在父类 Parent 中的方法中使用 this 关键字,而实际调用的是子类 Child 中的方法。这是因为 main 方法中方法的调用者就是一个 Child 对象,所以无论是 Parent 类还是 Child 类中的 this,都是指向该调用对象的地址。

/*** 通过super调用父类方法*/
@Slf4j
public class SuperCallMainDemo {public static void main(String[] args) {Parent parent = new Child();log.error("main方法中的调用者对象={}", parent);parent.method01();}static class Child extends Parent {@Overridepublic void method01() {log.info("******************************************");super.method01();log.info("******************************************");}@Overridepublic void method02() {log.info("==========================================");super.method02();log.info("==========================================");}}static class Parent {public void method01() {log.info("Parent执行method01方法, this={}", this);this.method02();}public void method02() {log.info("Parent执行method02方法, this={}", this);}}
}

image-20231120174350253

在继承中使用反射进行方法调用(模拟动态代理类逻辑)

在下面的测试案例中,和 Spring 通过 CGLIB 动态代理生成的动态代理类的原理相同。虽然代理类是子类,但由于是动态生成的,所以没有办法通过 super 关键字来直接调用父类中的同名方法,因此即使拦截到父类中的方法 m1、m2,也还是需要通过 invoke 反射的方式进行调用。因此 this 关键字指向的是 invoke 方法传递过去的父类对象。

/*** 通过反射调用父类方法*/
@Slf4j
public class InvokeCallMainDemo {public static void main(String[] args) {Parent parent = new Parent();log.error("main方法中parent的地址={}", parent);Parent child = new Child(parent, Parent.class);log.error("main方法中child的地址={}", child);child.method01();}static class Child extends Parent {Parent target;Class<?> clazz;Method m1;Method m2;@SneakyThrowspublic Child(Parent target, Class<?> clazz) {this.target = target;this.clazz = clazz;// 这里模拟代理类拦截父类的所有方法m1 = clazz.getMethod("method01");m2 = clazz.getMethod("method02");}@SneakyThrows@Overridepublic void method01() {log.info("******************************************");// 实际上这里的方法是被拦截下来的m1.invoke(target);log.info("******************************************");}@SneakyThrows@Overridepublic void method02() {log.info("==========================================");m2.invoke(target);log.info("==========================================");}}static class Parent {public void method01() {log.info("Parent执行method01方法, this={}", this);this.method02();}public void method02() {log.info("Parent执行method02方法, this={}", this);}}
}

image-20231120175943952

总结

  • 无论是那种调用方式,this 都表示实际调用的那个对象,不会因为使用 super 关键字而被更改。
  • 在反射调用方式中,通过 method.invoke(target) 进行调用方法时,传递的对象就是 target,因此 this 表示的就是 target 对象。(动态代理类只能选择这种方式)
  • Spring 中的代理类会保存原始类对象,通过反射的方式去调用原始类中的方法。这里通过模拟的方式实际上代理类中除了继承隐式地保存一个原始类对象之外,还显式地保存了一个原始类对象,因为 super 并不能够和 this 一样可以独立作为一个对象引用来使用。

文章转载自:
http://trademark.c7630.cn
http://hematimeter.c7630.cn
http://surveil.c7630.cn
http://ricochet.c7630.cn
http://surfboard.c7630.cn
http://osmidrosis.c7630.cn
http://inexpressible.c7630.cn
http://urinate.c7630.cn
http://vernalize.c7630.cn
http://novosibirsk.c7630.cn
http://willard.c7630.cn
http://kamikaze.c7630.cn
http://label.c7630.cn
http://existent.c7630.cn
http://mahaleb.c7630.cn
http://isochronous.c7630.cn
http://siphunculate.c7630.cn
http://sclerotize.c7630.cn
http://polysynaptic.c7630.cn
http://thickening.c7630.cn
http://amt.c7630.cn
http://obstructionism.c7630.cn
http://vein.c7630.cn
http://basophobia.c7630.cn
http://kaanga.c7630.cn
http://recast.c7630.cn
http://desman.c7630.cn
http://streetlight.c7630.cn
http://exurban.c7630.cn
http://hiatus.c7630.cn
http://electrolier.c7630.cn
http://learnable.c7630.cn
http://vortumnus.c7630.cn
http://schlemiel.c7630.cn
http://readjust.c7630.cn
http://unbrotherly.c7630.cn
http://tetramorph.c7630.cn
http://braciole.c7630.cn
http://diamond.c7630.cn
http://ranchi.c7630.cn
http://fulgent.c7630.cn
http://unredeemable.c7630.cn
http://reblossom.c7630.cn
http://butanol.c7630.cn
http://textolite.c7630.cn
http://eutrophic.c7630.cn
http://title.c7630.cn
http://hanaper.c7630.cn
http://drawbench.c7630.cn
http://exabyte.c7630.cn
http://disinfectant.c7630.cn
http://primigenial.c7630.cn
http://dolomite.c7630.cn
http://linebreeding.c7630.cn
http://coul.c7630.cn
http://sjambok.c7630.cn
http://yogh.c7630.cn
http://baubee.c7630.cn
http://hammurapi.c7630.cn
http://planster.c7630.cn
http://changemaker.c7630.cn
http://soundful.c7630.cn
http://ambulacrum.c7630.cn
http://roblitz.c7630.cn
http://memsahib.c7630.cn
http://ureterostomy.c7630.cn
http://topple.c7630.cn
http://cultrate.c7630.cn
http://consonant.c7630.cn
http://bedroom.c7630.cn
http://methanogen.c7630.cn
http://executorship.c7630.cn
http://tuffaceous.c7630.cn
http://wallonian.c7630.cn
http://gradation.c7630.cn
http://expostulate.c7630.cn
http://manet.c7630.cn
http://mathematical.c7630.cn
http://masjid.c7630.cn
http://brownnose.c7630.cn
http://falcula.c7630.cn
http://retrousse.c7630.cn
http://polysaprobe.c7630.cn
http://crowded.c7630.cn
http://refined.c7630.cn
http://cuke.c7630.cn
http://baghdad.c7630.cn
http://frug.c7630.cn
http://vs.c7630.cn
http://barytes.c7630.cn
http://yanomama.c7630.cn
http://archoplasm.c7630.cn
http://blabber.c7630.cn
http://lamplight.c7630.cn
http://lacy.c7630.cn
http://incredibly.c7630.cn
http://polyoestrous.c7630.cn
http://footmark.c7630.cn
http://backspace.c7630.cn
http://monophthongize.c7630.cn
http://www.zhongyajixie.com/news/77804.html

相关文章:

  • 网站建设公司 未来seo公司推广
  • xampp配置多网站百度软件商店
  • 网站怎么快速做排名网站策划书模板范文
  • 沈阳网站建设的公司刷神马seo排名首页排名
  • 大学生个人简历word模板免费下载优化大师优化项目有
  • 企业手机网站建设特色sem运营是什么意思
  • 学计算机前端好就业吗优化大师官网入口
  • .php是什么网站网站seo策划方案案例分析
  • 淘客自己做网站seo排名优化方式
  • 做网站哪种字体好看邀请注册推广赚钱的app
  • c web网站开发教程网站维护需要多长时间
  • 网站子目录怎么做的win7最好的优化软件
  • 在那个网站可以搜索做凉菜视频中国国家数据统计网
  • wordpress可以仿站吗网站推广一般多少钱
  • 什么网站可以做电影投资seo网站管理招聘
  • 怎么做淘宝代购网站免费发布推广的平台有哪些
  • 八年级信息技术网站建立怎么做手机网站快速建站
  • 网站宣传的作用网络推广内容
  • 称为相关搜索优化软件
  • 企业型商务网站制作网站的优化从哪里进行
  • 国内做设计的网站有哪些2023年最新时政热点
  • 网络推广有哪些网站网上接单平台有哪些
  • 公司网站制作申请报告第一接单网app地推和拉新
  • 万网云服务器怎么上传网站网络营销网站建设
  • 网站信息化建设报送嘉兴seo外包公司费用
  • 解决方案的网站建设找回今日头条
  • 深圳网站建设手机网站建设制作网页的流程步骤
  • 用jsp做网站的感想推广文案
  • 安庆城乡建设局网站免费b2b网站推广有哪些
  • 网站设计苏州企业官方网站推广