spring scoped代理bean [英] spring scoped proxy bean

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

问题描述

有人可以解释弹簧 @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 注释(或没有远程作用域代理)的会话范围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.

推荐答案

春季文档的第3.4.4.5节解释得非常好:

(请注意以下'userPreferences' bean定义,因为它是不完整的):

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

<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

<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实例的对象),并且足够聪明,能够从我们选择的任何底层作用域机制中获取真实的UserPreferences对象(HTTP请求) ,会议等)。然后我们可以安全地将这个代理对象注入到'userManager'bean中,该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.

这就是为什么在将request-,session-和globalSession-scoped 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 scoped代理bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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