spring框架中使用代理(动态代理)是什么意思?

发布于 2024-08-20 13:53:42 字数 32 浏览 14 评论 0原文

我不知道spring中使用代理的意义。什么是高效?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

同展鸳鸯锦 2024-08-27 13:53:42

代理由 AOP 使用。简而言之:

通常你有。

Caller --> Real object

但是,例如,当您需要自动事务管理时,Spring 会

Caller --> Proxy --> Real object

在代理启动事务的地方放置真实对象的代理。

这是一篇不错的文章解释了两者的本质春季代理的数量及其效率(性能)

Proxies are used by AOP. In short:

Normally you have.

Caller --> Real object

But when, for example, you want automatic transaction management, spring puts a proxy of your real object

Caller --> Proxy --> Real object

where the proxy starts the transaction.

Here is nice article explaining both the essence of proxies and their efficiency (performance) in spring

浅暮の光 2024-08-27 13:53:42

动态代理是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 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.

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

怎会甘心 2024-08-27 13:53:42

我们可以通过修改源/字节代码或使用嵌入附加功能并将调用委托给底层对象的子类或代理来向 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.

凉风有信 2024-08-27 13:53:42

其他答案都很好,但这就是我如何以非常简单的方式思考它。

  1. 注释的意思是“为额外行为添加隐藏代码”。
  2. 例如,框架(或任何知道注释含义的框架)在运行时添加字节码、Spring,在编译时添加 AspectJ。
  3. 它添加代码作为代理和拦截器。 (包装器、装饰器、适配器很相似,可能比“代理”更容易理解。)
  4. 当程序运行时,拦截器将执行发送到执行其任务的代理,然后代理可能会也可能不会将执行发送到该类你编码并且它“包装”。

The other answers are good, but here's how I think of it in very simple terms.

  1. An annotation means "add hidden code for extra behavior."
  2. The framework (or whatever knows what the annotation means) adds bytecode, Spring at runtime, AspectJ at compile time, for example.
  3. It adds code as a proxy along with an interceptor. (A wrapper, decorator, adapter are similar and may be easier to understand than "proxy".)
  4. 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".
落墨 2024-08-27 13:53:42

AOP 还可以使用 CGLIB 代理。这用于代理类而不是接口。

AOP can also use CGLIB proxies. This is used to proxy the classes instead of interfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文