在spring框架中使用代理(动态代理)是什么意思? [英] What is the meaning of using proxy ( dynamic proxy) in spring framework?

查看:364
本文介绍了在spring框架中使用代理(动态代理)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道在春天使用代理的意义。什么是有效的?

I don't know the meaning of using proxy in spring. what is efficient?

推荐答案

动态代理是JDK的一项功能。它可用于使用调用来实现接口处理程序

The dynamic proxy is a feature of the JDK. It can be used to implement an interface using an invocation handler.


动态代理类(以下简称
作为代理类)是$ class b $ b实现了在创建
类时在运行时指定的接口
的列表,其行为如下所述
。代理接口是一个
接口,由
代理类实现。代理实例是代理类的
实例。每个代理
实例都有一个关联的调用
处理程序对象,它实现
接口InvocationHandler。

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.

动态代理有一些开销。但是,对于大多数用例来说,开销并不重要。真正的问题是动态代理的(过度)使用使得应用程序更难理解和调试。例如,动态代理将在堆栈跟踪中显示多行。

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.

动态代理通常用于实现装饰者。其中一个例子是Spring的AOP。 (我不想详细介绍 AOP ,也不会使用AOP保持简单的术语)。某些问题在一个类别中实施并在许多地方使用。动态代理(和调用处理程序)只是用于拦截方法调用的粘合代码(由Spring提供)。 (实际上,动态代理只是此功能的一个实现细节。动态生成类是实现它的另一种可能性。)

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.)

这篇关于在spring框架中使用代理(动态代理)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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