spring 作用域代理 bean [英] spring scoped proxy bean

查看:24
本文介绍了spring 作用域代理 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下spring@ScopedProxy注解的用法?我认为这与会话范围的 bean 有关,但我不太确定是什么.

Can someone explain the usage of the spring @ScopedProxy annotation? I thought it had something to do with session scoped beans, but I'm not quite sure what.

在我使用范围时,我使用了没有 @ScopedProxy 注释(或没有 aop 范围代理)的会话范围 bean,所以我非常确定如何正确使用它.

In my usage of scopes, I've used session scoped beans without the @ScopedProxy annotation (or without aop scoped proxies), so I'm really sure how to use it properly.

推荐答案

spring 文档的第 3.4.4.5 节 很好地解释了它:

(请注意以下userPreferences"bean 定义不完整):

(please note that the following 'userPreferences' bean definition as it stands is incomplete):

<!-- an HTTP Session-scoped bean -->
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

<!-- a singleton-scoped bean -->
<bean id="userManager" class="com.foo.UserManager">
    <property name="userPreferences" ref="userPreferences"/>
</bean>

从上面的配置可以明显看出,单例 bean 'userManager' 被注入了对 HTTP 会话范围的 bean 'userPreferences' 的引用.这里的重点是 'userManager' bean 是一个单例...它将每个容器只实例化一次及其依赖项(在本例中只有一个,即userPreferences"bean)也只会被注入(一次!).

From the above configuration it is evident that the singleton bean 'userManager' is being injected with a reference to the HTTP Session-scoped bean 'userPreferences'. The salient point here is that the 'userManager' bean is a singleton... it will be instantiated exactly once per container, and its dependencies (in this case only one, the 'userPreferences' bean) will also only be injected (once!).

这意味着userManager"将(概念上)只对完全相同的userPreferences"对象进行操作,这是它最初注入的对象.

This means that the 'userManager' will (conceptually) only ever operate on the exact same 'userPreferences' object, that is the one that it was originally injected with.

当您将 HTTP 会话范围的 bean 作为依赖项注入协作对象(通常)时,这不是您想要的.相反,我们想要的是每个容器有一个userManager"对象,然后,对于 HTTP 会话的生命周期,我们希望查看并使用特定于所述 HTTP 会话的userPreferences"对象.

This is not what you want when you inject a HTTP Session-scoped bean as a dependency into a collaborating object (typically). Rather, what we do want is a single 'userManager' object per container, and then, for the lifetime of a HTTP Session, we want to see and use a 'userPreferences' object that is specific to said HTTP Session.

相反,您需要的是注入某种对象,该对象公开与 UserPreferences 类完全相同的公共接口(理想情况下是一个 UserPreferences 实例的对象),并且该对象足够智能,能够离开并获取来自我们选择的任何底层范围机制(HTTP 请求、会话等)的真实 UserPreferences 对象.然后我们可以安全地将这个代理对象注入到userManager"bean 中,它不会意识到它持有的 UserPreferences 引用是一个代理.

Rather what you need then is to inject some sort of object that exposes the exact same public interface as the UserPreferences class (ideally an object that is a UserPreferences instance) and that is smart enough to be able to go off and fetch the real UserPreferences object from whatever underlying scoping mechanism we have chosen (HTTP request, Session, etc.). We can then safely inject this proxy object into the 'userManager' bean, which will be blissfully unaware that the UserPreferences reference that it is holding onto is a proxy.

在我们的例子中,当 UserManager 实例调用依赖注入的 UserPreferences 对象上的方法时,它实际上会调用代理上的方法...然后代理将关闭并从(在本例中)HTTP 会话中获取真实的 UserPreferences 对象,并将方法调用委托给检索到的真实 UserPreferences 对象.

In our case, when a UserManager instance invokes a method on the dependency-injected UserPreferences object, it will really be invoking a method on the proxy... the proxy will then go off and fetch the real UserPreferences object from (in this case) the HTTP Session, and delegate the method invocation onto the retrieved real UserPreferences object.

这就是为什么在将请求范围、会话范围和全局会话范围的 bean 注入协作对象时需要以下正确且完整的配置:

That is why you need the following, correct and complete, configuration when injecting request-, session-, and globalSession-scoped beans into collaborating objects:

<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
    <aop:scoped-proxy/>
</bean>

<bean id="userManager" class="com.foo.UserManager">
    <property name="userPreferences" ref="userPreferences"/>
</bean>

这篇关于spring 作用域代理 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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