当前位置:网站首页>使用代理对象执行实现类目标方法异常
使用代理对象执行实现类目标方法异常
2022-08-04 22:47:00 【DanceDonkey】
使用JDK方式创建动态代理对象时,通常都是会强转为目标接口类型。
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的实例,此时以接口的形式来调用方法调用的时接口的方法是可以的,什么意思? 可参考如下代码
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 是实现类,获取了接口的a4方法和实现类型的a4方法,然后创建了BaseController的一个实现类,用实现类去分别反射调用这两个method,发现调用成功了。

但是如果是使用创建的代理对象去执行实现类的方法则会抛出异常。
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");
}

这个其实也可以很好想明白target是一个代理对象,是BaseController的代理对象,本身和BaseControllerImpl是没有关系的,他俩都实现了BaseController接口,因此可以说是兄弟关系,但是兄弟之间就不能实现相同方法的调用了,必须得是父类或自身得方法可以。所以在JDK实现的代理中,在调用接口目标方法时,是调用的接口的方法。
可通过debug查看在代理对象调用方法时的method签名,可以很明显看到是接口中的方法签名。对于Spring中声明的切面来说,切面类并不能生成代理对象,只不过是通知Spring根据切面配置的pointcut来检测bean是否需要创建代理对象,用哪种方式创建等,而切面类声明的方法只是用来告诉Spring是哪种通知类型。

边栏推荐
猜你喜欢
![[Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems](/img/21/594260a3b98c73916ebc535d0f81e4.png)
[Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems

测试薪资这么高?刚毕业20K,仅需3.5个月

Lecture 2 Software Life Cycle

【游戏建模模型制作全流程】使用ZBrush制作骷髅王

【TCP/IP 四 IP 网际协议】

Pytest学习-Fixture

线上虚拟展馆展示具有哪些优势
![[Mock Interview - 10 Years of Work] Are more projects an advantage?](/img/fa/2652629d1ff4653aca0d626ac89bf8.jpg)
[Mock Interview - 10 Years of Work] Are more projects an advantage?

xss总结

js中小数四则运算精度问题原因及解决办法
随机推荐
Service Mesh落地路径
限制tensorflow使用Cpu核数
Linux系统重启和停止Mysql服务教程
3D激光SLAM:LeGO-LOAM---两步优化的帧间里程计及代码分析
期货开户哪个平台好,要正规安全的
How to make a video gif?Try this video making gif artifact
go语言的time包介绍
【3D建模制作技巧分享】ZBrush如何重新拓扑
【3D建模制作技巧分享】ZBrush如何使用Z球
promise详解
【论文笔记KDD2021】MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
基于事实的讨论
JVM内存配置参数GC日志
xss总结
【2020】【Paper Notes】Metasurfaces: Multifunctional and Programmable——
panic: reflect: reflect.Value.SetString using value obtained using unexported field
synchronized和ReentrantLock都很丝滑,因为他们都是可重入锁,一个线程多次拿锁也不会死锁,我们需要可重入
质量管理大师爱德华·戴明博士经典的质量管理14条原则
测试薪资这么高?刚毕业20K,仅需3.5个月
智慧养老整体解决方案