java 具体类的动态代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7878521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:47:46  来源:igfitidea点击:

Dynamic proxy for concrete classes

javareflectiondynamic-proxy

提问by eliocs

I want to define a method interceptor in a Java program in other words I want to have a behaviour which is executed at each method call. This application isn't executed in an application server and therefore I can't use the EJB around invoke interceptors. I have found a nice ProxyAPI in the standard Java libraries but its limited because it needs an interface in the proxy creation:

我想在 Java 程序中定义一个方法拦截器,换句话说我想有一个在每个方法调用时执行的行为。此应用程序不在应用程序服务器中执行,因此我无法围绕调用拦截器使用 EJB。我在标准 Java 库中找到了一个不错的代理API,但它是有限的,因为它在代理创建中需要一个接口:

 Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                                      new Class[] { Foo.class },
                                      handler);

Is there a similar API which doesn't force Foo.class to be declared as an interface?

是否有类似的 API 不强制将 Foo.class 声明为接口?

采纳答案by Brian Agnew

Why not use CGLIB? See this articlefor more information.

为什么不使用CGLIB?有关更多信息,请参阅此文章

What if you want to proxy legacy classes that do not have interfaces? You can use CGLIB. CGLIB is a powerful, high-performance code generation library. Under the cover, it uses ASM, a small but fast bytecode manipulation framework, to transform existing byte code to generate new classes. CGLIB is faster than the JDK dynamic proxy approach. Essentially, it dynamically generates a subclass to override the non-final methods of the proxied class and wires up hooks that call back to the user-defined interceptors.

如果要代理没有接口的遗留类怎么办?您可以使用 CGLIB。CGLIB 是一个强大的、高性能的代码生成库。在幕后,它使用 ASM(一个小而快速的字节码操作框架)来转换现有的字节码以生成新的类。CGLIB 比 JDK 动态代理方法更快。本质上,它动态生成一个子类来覆盖代理类的非最终方法,并连接回调用户定义的拦截器的钩子。

回答by Tomasz Nurkiewicz

Unfortunately there is no such API for classes. Many frameworks are using bytecode generation libraries like CGLIB to achieve this.

不幸的是,类没有这样的 API。许多框架正在使用像 CGLIB 这样的字节码生成库来实现这一点。

回答by Peter Lawrey

You can try one of the mocking classes. The simplest approach may be to sub-class, your class. Or you could use AOP to inject the logging code you want.

您可以尝试其中一种模拟课程。最简单的方法可能是子类,你的类。或者您可以使用 AOP 注入您想要的日志记录代码。

回答by Chris Hennick

sun.misc.ProxyGenerator can be used to generate proxy classes and doesn't check that their "interfaces" are all interfaces. Its generateClassFile method gives you the bytecode as a byte array, which you can save to link into future builds or alter with third-party tools.

sun.misc.ProxyGenerator 可用于生成代理类,并且不检查它们的“接口”是否都是接口。它的 generateClassFile 方法以字节数组的形式为您提供字节码,您可以将其保存以链接到未来的构建中或使用第三方工具进行更改。

Note that if any of the "interfaces" has a final method, you'll get an error when you try to load the class.

请注意,如果任何“接口”具有 final 方法,则在尝试加载类时会出现错误。