什么是com.sun.proxy。$ Proxy [英] What is com.sun.proxy.$Proxy

查看:2944
本文介绍了什么是com.sun.proxy。$ Proxy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到,当错误发生在不同的框架中时(例如,实现EJB规范的框架或一些JPA提供程序),堆栈跟踪包含类似 com.sun.proxy的类。$ Proxy 。我知道什么是代理,但我正在寻找更具技术性和更具体的java答案。

I have seen that when errors occur deep in different frameworks (e.g frameworks implementing the EJB specification or some JPA providers) the stacktrace contain classes like com.sun.proxy.$Proxy. I know what a Proxy is, but I am looking for a more technical and more java specific answer.


  1. 它们是什么?

  2. 它们是如何创建的?

  3. 与JVM有什么关系?它们是JVM实现特定的吗?


推荐答案


  1. Proxies是在运行时创建和加载的类。这些类没有源代码。我知道你想知道如果没有代码可以让他们做点什么。答案是,在创建它们时,指定一个实现 InvocationHandler 的对象,它定义了在调用代理方法时调用的方法。

  1. Proxies are classes that are created and loaded at runtime. There is no source code for these classes. I know that you are wondering how you can make them do something if there is no code for them. The answer is that when you create them, you specify an object that implements InvocationHandler, which defines a method that is invoked when a proxy method is invoked.

您可以使用调用创建它们

You create them by using the call

Proxy.newProxyInstance(classLoader, interfaces, invocationHandler)

参数为:


  1. classLoader 。生成类后,将使用此类加载器加载它。

  2. interfaces 。必须全部是接口的类对象数组。生成的代理实现了所有这些接口。

  3. invocationHandler 。这是您的代理在调用方法时知道该怎么做的方式。它是一个实现 InvocationHandler 的对象。当来自任何受支持的接口的方法,或 hashCode 等于 toString <调用/ code>,在处理程序上调用方法 invoke ,传递方法对象要调用的方法和传递的参数。

  1. classLoader. Once the class is generated, it is loaded with this class loader.
  2. interfaces. An array of class objects that must all be interfaces. The resulting proxy implements all of these interfaces.
  3. invocationHandler. This is how your proxy knows what to do when a method is invoked. It is an object that implements InvocationHandler. When a method from any of the supported interfaces, or hashCode, equals, or toString, is invoked, the method invoke is invoked on the handler, passing the Method object for the method to be invoked and the arguments passed.

有关详细信息,请参阅 代理 类。

For more on this, see the documentation for the Proxy class.

版本1.3之后的每个JVM实现都必须支持这些。它们以特定于实现的方式加载到JVM的内部数据结构中,但保证可以正常工作。

Every implementation of a JVM after version 1.3 must support these. They are loaded into the internal data structures of the JVM in an implementation-specific way, but it is guaranteed to work.

这篇关于什么是com.sun.proxy。$ Proxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆