当前位置:网站首页>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.
边栏推荐
- ClickHouse 二级索引
- ANT1.7 download and configuration method
- The Controller layer code is written like this, concise and elegant!
- 生产者消费者问题
- 容联云发送短信验证码
- Service Mesh landing path
- Will we still need browsers in the future?(feat. Maple words Maple language)
- 正则表达式绕过
- 请你说一下final关键字以及static关键字
- 【3D建模制作技巧分享】ZBrush如何设置笔刷快捷键
猜你喜欢
CS8416国产替代DP8416 数字音频接收器
SSM整合完整流程讲解
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
[Mock Interview - 10 Years of Work] Are more projects an advantage?
If you can't get your heart, use "distributed lock" to lock your people
【3D建模制作技巧分享】在zbrush中如何雕刻头发 ZBrush头发雕刻小技巧
智慧养老整体解决方案
一点点读懂cpufreq(一)
【3D建模制作技巧分享】ZBrush如何使用Z球
Since a new byte of 20K came out, I have seen what the ceiling is
随机推荐
特征工程资料汇总
应用联合、体系化推进。集团型化工企业数字化转型路径
golang打开文件和读写文件
【3D建模制作技巧分享】zbrush贴图映射小技巧
C语言实现扫雷 附带源代码
请你说一下final关键字以及static关键字
enumerate()函数
Service Mesh landing path
ANT1.7 download and configuration method
PID Controller Improvement Notes No. 7: Improve the anti-overshoot setting of the PID controller
Shell expect 实战案例
CS8416国产替代DP8416 数字音频接收器
2022/8/4 树上差分+线段树
如何根据地址获取函数名
2022年全网最全接口自动化测试框架搭建,没有之一
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
The Record of Reminding myself
直播带货为农产品开拓销售渠道
App测试和Web测试的区别
养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习