spring框架中使用代理(动态代理)是什么意思?
我不知道spring中使用代理的意义。什么是高效?
I don't know the meaning of using proxy in spring. what is efficient?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我不知道spring中使用代理的意义。什么是高效?
I don't know the meaning of using proxy in spring. what is efficient?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
代理由 AOP 使用。简而言之:
通常你有。
但是,例如,当您需要自动事务管理时,Spring 会
在代理启动事务的地方放置真实对象的代理。
这是一篇不错的文章解释了两者的本质春季代理的数量及其效率(性能)
Proxies are used by AOP. In short:
Normally you have.
But when, for example, you want automatic transaction management, spring puts a proxy of your real object
where the proxy starts the transaction.
Here is nice article explaining both the essence of proxies and their efficiency (performance) in spring
动态代理是JDK。它可用于使用 调用来实现接口处理程序。
动态代理有一些开销。不过,对于大多数用例来说,开销不会很大。真正的问题是(过度)使用动态代理会使应用程序更难理解和调试。例如,动态代理将在堆栈跟踪中显示多行。
动态代理通常用于实现装饰器。 Spring 中的 AOP 就是一个例子。 (我不想详细讨论 AOP 并且不会使用 AOP术语以使事情简单)。某些关注点在一个类中实现并在多个地方使用。动态代理(和调用处理程序)只是拦截方法调用的粘合代码(由 Spring 提供)。 (实际上,动态代理只是此功能的一个实现细节。动态生成类是实现它的另一种可能性。)
The dynamic proxy is a feature of the JDK. It can be used to implement an interface using an invocation handler.
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 AOP and 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.)
我们可以通过修改源/字节代码或使用嵌入附加功能并将调用委托给底层对象的子类或代理来向 Java 类添加功能。
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.
其他答案都很好,但这就是我如何以非常简单的方式思考它。
The other answers are good, but here's how I think of it in very simple terms.
AOP 还可以使用 CGLIB 代理。这用于代理类而不是接口。
AOP can also use CGLIB proxies. This is used to proxy the classes instead of interfaces.