当前位置:网站首页>Implementing class target method exception using proxy object execution
Implementing class target method exception using proxy object execution
2022-08-04 23:06:00 【DanceDonkey】
使用JDKway when creating a dynamic proxy object,Usually it will be cast to the target interface type.
public class Test {
public static void main(String[] args) throws Exception{
BaseController baseController = new BaseControllerImpl();
InvocationHandler handler = new MyInvocationHandler(baseController);
BaseController target = (BaseController) Proxy.newProxyInstance(BaseController.class.getClassLoader(), new Class[]{
BaseController.class}, handler);
target.a4("111");
}
}
此时的target就是一个代理对象,同时是Proxy与BaseController的实例,At this time, it is possible to call the method of the interface when the method is called in the form of an interface,什么意思? 可参考如下代码
public static void main(String[] args)throws Exception {
Method a4 = BaseController.class.getMethod("a4", String.class);
Method a4Impl = BaseControllerImpl.class.getMethod("a4", String.class);
BaseControllerImpl baseController = new BaseControllerImpl();
a4Impl.invoke(baseController,"111");
a4.invoke(baseController,"222");
}
BaseController是接口,BaseControllerImpl 是实现类,got the interfacea4method and implementation typea4方法,然后创建了BaseController的一个实现类,Use the implementation class to invoke the two reflections separatelymethod,The call was found to be successful.
However, if the created proxy object is used to execute the method of the implementation class, an exception will be thrown.
public static void main(String[] args) throws Exception{
BaseController baseController = new BaseControllerImpl();
InvocationHandler handler = new MyInvocationHandler(baseController);
BaseController target = (BaseController) Proxy.newProxyInstance(BaseController.class.getClassLoader(), new Class[]{
BaseController.class}, handler);
Method a4 = BaseController.class.getMethod("a4", String.class);
Method a4Impl = BaseControllerImpl.class.getMethod("a4", String.class);
a4.invoke(target,"111");
a4Impl.invoke(target,"222");
}
This can actually be well understoodtarget是一个代理对象,是BaseController的代理对象,本身和BaseControllerImpl是没有关系的,They both achieved itBaseController接口,So it can be said to be a sibling relationship,However, the same method calls cannot be implemented between brothers,It must be the parent class or its own method.所以在JDKin the implemented proxy,When calling the interface target method,is a method of the interface to call.
可通过debugSee when the method is called on the proxy objectmethod签名,It is obvious that it is the method signature in the interface.对于SpringIn terms of the aspect declared in ,Aspect classes do not generate proxy objects,Just notificationSpringConfigured according to the aspectpointcut来检测bean是否需要创建代理对象,In which way to create etc,The method declared by the aspect class is only used to tellSpringWhich notification type it is.
边栏推荐
- 学生管理系统架构设计
- 基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
- FinClip崁入式搭建生态平台,降低合作门槛
- 测试技术:关于上下文驱动测试的总结
- 【字符串函数内功修炼】strcpy + strcat + strcmp(一)
- 今天又做了三个梦,其中一个梦梦里的我还有意识会思考?
- PAN3020 Sub-1G无线收发芯片
- 3年,从3K涨薪到20k?真是麻雀啄了牛屁股 — 雀食牛逼呀
- 现在学习次世代3D游戏建模还能找到高薪好工作吗
- TypeScript - the use of closure functions
猜你喜欢
【游戏建模模型制作全流程】ZBrush蜥蜴模型雕刻教程
【2020】【论文笔记】超表面:多功能和可编程——
【3D建模制作技巧分享】ZBrush如何重新拓扑
SSM整合完整流程讲解
功耗控制之DVFS介绍
enumerate()函数
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
【字符串函数内功修炼】strncpy + strncat + strncmp(二)
【3D建模制作技巧分享】ZBrush如何使用Z球
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
随机推荐
Both synchronized and ReentrantLock are smooth, because they are reentrant locks, and a thread will not deadlock if it takes the lock multiple times. We need reentrant locks
js中小数四则运算精度问题原因及解决办法
FinClip崁入式搭建生态平台,降低合作门槛
【3D建模制作技巧分享】如何使用ZBrush导出效果图
Kernel函数解析之kernel_restart
Nacos配置中心之客户端长轮询
【3D建模制作技巧分享】Maya模型如何导入zbrush
一招包治pycharm DEBUG报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode
SRv6网络的安全解决方案
Reconfigure the ffmpeg plugin in chrome
【2020】【论文笔记】超表面:多功能和可编程——
【无标题】
Pytest learning - fixtures
逆序对的数量
亿流量大考(3):不加机器,如何抗住每天百亿级高并发流量?
一点点读懂thermal(一)
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
Acwing3593. 统计单词
Qt中的常用控件
论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》