如何在java中创建代理 [英] How to create proxy in java
问题描述
他们如何在java中创建类
的代理。他们是否根据需要创建代理,或者他们创建代理并永远使用它。
How do they create proxy of a class
in java. Do they create the proxy on as needed basis or they create it and have it around forever.
推荐答案
您可以在两个中创建代理基本方法:
You can create proxies in two basic ways:
- 使用JDK机制,按接口。看看
java .lang.reflect.Proxy
。 - 在一个具体的类上使用一些字节码生成/操作库。请查看 cglib 和 javassist
- using the JDK mechanism, by interface. Take a look at
java.lang.reflect.Proxy
. - using some byte-code generation/manipulation library, on a concrete class. Take a look at cglib and javassist
Apache有一个很好的实用工具:< a href =http://commons.apache.org/proxy/ =noreferrer> commons-proxy 。它包含了许多用于创建代理的机制和框架。
Apache has a nice utility: commons-proxy. It wraps many mechanisms and frameworks for creating proxies.
这完全是关于动态代理 - 即在运行时创建的代理。对于静态代理 - 请参阅有关代理模式的维基百科
This is all about dynamic proxies - i.e. those created at runtime. For static proxies - see wikipedia about the proxy pattern
请注意,您不是在创建一个类的代理 - 您正在创建一个对象的代理。
Note that you are not making a proxy of a class - you are making a proxy of an object.
这篇关于如何在java中创建代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!