简体   繁体   English

Java动态代理问题

[英]Java dynamic proxy questions

1.Does dynamic proxy instance subclass the target class? 1.动态代理实例是否为目标类的子类? The java doc says the proxy instance implements "a list of interfaces", says nothing about subclassing, but through debugging, I saw that the proxy instance did inherit the target class properites.What does the "a list of interfaces " mean? java doc说代理实例实现了“接口列表”,没有提到子类化,但是通过调试,我看到代理实例确实继承了目标类的属性。“接口列表”是什么意思? Can I exclude those interfaces implemented by target class ? 我可以排除目标类实现的那些接口吗?

2.Can I invoke target class specific methods on a proxy instance? 2.我可以在代理实例上调用特定于目标类的方法吗?

3. I think dynamic proxy is an interface methods invocation proxy but rather than a target class proxy, is that right (I am deeply infected by hibernate proxy object concept)? 3.我认为动态代理是一个接口方法调用代理而不是目标类代理,是不是(我深受hibernate代理对象概念的感染)?

If you're talking about the java.lang.reflect.Proxy class: there's no such thing as a "target class" in general. 如果你在谈论java.lang.reflect.Proxy类:一般来说,没有“目标类”这样的东西。

The proxy is constructed by specifying a list of interfaces that the proxy object will implement, and an invocation handler whose invoke() method all method calls on the proxy will be forwarded to. 通过指定代理对象将实现的接口列表以及调用处理程序来构造代理,该调用处理程序的invoke()方法将对代理上的所有方法调用进行转发。 The invocation handler can do anything with them, including forwarding them to a "target class" instance it holds a reference to. 调用处理程序可以对它们执行任何操作,包括将它们转发到它拥有引用的“目标类”实例。

  1. I think you have misunderstood. 我觉得你误解了。 Every Class object passed to getProxyClass() must be a class object for an interface, not a concrete class. 传递给getProxyClass()的每个Class对象必须是接口的类对象,而不是具体的类。 So String.class would not be a valid argument but List.class would be. 所以String.class不是一个有效的参数,但List.class会是。 As it says "All of the Class objects in the interfaces array must represent interfaces, not classes or primitive types". 正如它所说的那样“接口数组中的所有Class对象必须代表接口,而不是类或基本类型”。 As a result subclasses are irrelevant. 因此,子类是无关紧要的。

  2. No (as classes are not relevant here, only interfaces). 否(因为类在这里不相关,只有接口)。 If you need to access them add an interface. 如果您需要访问它们,请添加一个界面。

  3. Correct. 正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM