Java spring框架中使用proxy(动态代理)是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2227836/
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
What is the meaning of using proxy ( dynamic proxy) in spring framework?
提问by Linh
I don't know the meaning of using proxy in spring. what is efficient?
我不知道在spring中使用代理的含义。什么是有效的?
采纳答案by Thomas Jung
The dynamic proxyis a feature of the JDK. It can be used to implement an interface using an invocation handler.
该动态代理是JDK的一个特点。它可用于使用调用处理程序实现接口。
A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below. A proxy interface is such an interface that is implemented by a proxy class. A proxy instance is an instance of a proxy class. Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler.
动态代理类(以下简称为代理类)是一个类,它实现了在创建类时在运行时指定的接口列表,其行为如下所述。代理接口就是这样一个由代理类实现的接口。代理实例是代理类的实例。每个代理实例都有一个关联的调用处理程序对象,它实现了接口 InvocationHandler。
A dynamic proxy has some overhead. For most use cases the overhead won't be significant, though. The real problem is that the (over)use of dynamic proxies makes an application harder to understand and debug. For example a dynamic proxy will show up with mulitple lines in a stacktrace.
动态代理有一些开销。但是,对于大多数用例,开销不会很大。真正的问题是(过度)使用动态代理会使应用程序更难理解和调试。例如,动态代理将在堆栈跟踪中显示多行。
Dynamic proxies are often used to implement decorators. One example of this is AOP in Spring. (I don't want to go into the details of AOPand won't use AOP terminology to keep things simple). Where certain concerns are implemented in one class and used in many places. The dynamic proxies (and invocation handlers) are only the glue code (provided by Spring) to intercept the method calls. (Actually, dynamic proxies are only an implementation detail of this capability. Generating classes on the fly is another possibility to implement it.)
动态代理通常用于实现装饰器。这方面的一个例子是 Spring 中的 AOP。(我不想深入AOP的细节,也不会使用 AOP 术语来保持简单)。某些关注点在一个类中实现并在许多地方使用。动态代理(和调用处理程序)只是用于拦截方法调用的胶水代码(由 Spring 提供)。(实际上,动态代理只是此功能的一个实现细节。动态生成类是实现它的另一种可能性。)
回答by Bozho
Proxies are used by AOP. In short:
AOP使用代理。简而言之:
Normally you have.
通常你有。
Caller --> Real object
But when, for example, you want automatic transaction management, spring puts a proxy of your real object
但是,例如,当您想要自动事务管理时,spring 会放置您真实对象的代理
Caller --> Proxy --> Real object
where the proxy starts the transaction.
代理开始交易的地方。
Here is nice articleexplaining both the essence of proxies and their efficiency (performance) in spring
这是一篇很好的文章,解释了代理的本质及其在 spring 中的效率(性能)
回答by Sanjeev Kumar Dangi
We can add a functionality to Java class by modifying the source/byte code or by using subclass or proxy which embeds the additional functionality and delegates the calls to underlying object.
我们可以通过修改源代码/字节代码或使用嵌入附加功能并将调用委托给底层对象的子类或代理来向 Java 类添加功能。
回答by Nilesh
AOP can also use CGLIB proxies. This is used to proxy the classes instead of interfaces.
AOP 也可以使用 CGLIB 代理。这用于代理类而不是接口。
回答by allenjom
The other answers are good, but here's how I think of it in very simple terms.
其他答案都很好,但这是我用非常简单的术语来思考的。
- An annotation means "add hidden code for extra behavior."
- The framework (or whatever knows what the annotation means) adds bytecode, Spring at runtime, AspectJ at compile time, for example.
- It adds code as a proxy along with an interceptor. (A wrapper, decorator, adapter are similar and may be easier to understand than "proxy".)
- When the program runs, the interceptor sends execution to the proxy which does its thing, which then may or may not send execution to the class that you coded and that it "wraps".
- 注释的意思是“为额外行为添加隐藏代码”。
- 例如,框架(或任何知道注释意味着什么的东西)在运行时添加字节码、Spring、在编译时添加 AspectJ。
- 它添加了代码作为代理以及拦截器。(包装器、装饰器、适配器类似,可能比“代理”更容易理解。)
- 当程序运行时,拦截器将执行发送到执行其操作的代理,然后代理可能会或可能不会将执行发送到您编码并“包装”的类。
回答by Pramod Gautam
Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice). If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used. All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.
Spring AOP 使用 JDK 动态代理或 CGLIB 为给定的目标对象创建代理。(如果您有选择,则首选 JDK 动态代理)。如果要代理的目标对象至少实现了一个接口,则将使用 JDK 动态代理。目标类型实现的所有接口都将被代理。如果目标对象没有实现任何接口,那么将创建一个 CGLIB 代理。