有什么理由代理单例 bean 吗?
Is there any reason to proxy a singleton bean?
我在代码中看到了以下定义:
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class SomeComponent {
<...>
}
这是一个单例作用域 bean。在我看来,没有理由代理它。据我所知,我们应该代理一个 bean,以防它的范围不是单例并且不同于我们要注入它的另一个 bean 的范围。
可能是我什么都不知道。代理单身人士的原因可能是什么?
经过调查,我在 an outdated Spring's documentation 中找到了问题的答案:
You do not need to use the <aop:scoped-proxy/>
in conjunction with beans that are scoped as singletons or prototypes. It is an error to try to create a scoped proxy for a singleton bean (and the resulting BeanCreationException
will certainly set you straight in this regard).
它回答了我的问题,因为 <aop:scoped-proxy/>
是 @Scope
注释的替代方法。但是 current documentation 与旧的不同:
You may also use <aop:scoped-proxy/>
between beans that are scoped as singleton, with the reference then going through an intermediate proxy that is serializable and therefore able to re-obtain the target singleton bean on deserialization.
这意味着,如果您尝试定义单例范围代理,现在您将看不到 BeanCreationException
。但无论如何,我并不完全理解这个模糊用例的目的。所以,如果有人明白这句话的意思,请在评论中澄清。
结论:在大多数情况下,没有理由代理单例范围的 bean。
关于作用域代理:这是Spring documentation中所写的。
Note:
You do not need to use the in conjunction with beans that are scoped as singletons or prototypes. It is an error to try to create a scoped proxy for a singleton bean (and the resulting BeanCreationException will certainly set you straight in this regard).
虽然它指的是一个很旧的版本,但该注释在 IMO 中仍然有效。不过我从来没有自己尝试过。
话虽如此,代理单例 bean 的原因可能有很多,"proxy" 是一个非常广泛的概念。例如,当我们使用 @Transactional
作为代理时,还有很多其他的例子。问题是这不是 "scoped proxy",它可以被视为一种非常特殊的代理,显然不适用于单身人士。
我在代码中看到了以下定义:
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class SomeComponent {
<...>
}
这是一个单例作用域 bean。在我看来,没有理由代理它。据我所知,我们应该代理一个 bean,以防它的范围不是单例并且不同于我们要注入它的另一个 bean 的范围。
可能是我什么都不知道。代理单身人士的原因可能是什么?
经过调查,我在 an outdated Spring's documentation 中找到了问题的答案:
You do not need to use the
<aop:scoped-proxy/>
in conjunction with beans that are scoped as singletons or prototypes. It is an error to try to create a scoped proxy for a singleton bean (and the resultingBeanCreationException
will certainly set you straight in this regard).
它回答了我的问题,因为 <aop:scoped-proxy/>
是 @Scope
注释的替代方法。但是 current documentation 与旧的不同:
You may also use
<aop:scoped-proxy/>
between beans that are scoped as singleton, with the reference then going through an intermediate proxy that is serializable and therefore able to re-obtain the target singleton bean on deserialization.
这意味着,如果您尝试定义单例范围代理,现在您将看不到 BeanCreationException
。但无论如何,我并不完全理解这个模糊用例的目的。所以,如果有人明白这句话的意思,请在评论中澄清。
结论:在大多数情况下,没有理由代理单例范围的 bean。
关于作用域代理:这是Spring documentation中所写的。
Note:
You do not need to use the in conjunction with beans that are scoped as singletons or prototypes. It is an error to try to create a scoped proxy for a singleton bean (and the resulting BeanCreationException will certainly set you straight in this regard).
虽然它指的是一个很旧的版本,但该注释在 IMO 中仍然有效。不过我从来没有自己尝试过。
话虽如此,代理单例 bean 的原因可能有很多,"proxy" 是一个非常广泛的概念。例如,当我们使用 @Transactional
作为代理时,还有很多其他的例子。问题是这不是 "scoped proxy",它可以被视为一种非常特殊的代理,显然不适用于单身人士。