简体   繁体   English

spring 作用域代理 bean

[英]spring scoped proxy bean

Can someone explain the usage of the spring @ScopedProxy annotation?有人可以解释一下 spring @ScopedProxy注释的用法吗? I thought it had something to do with session scoped beans, but I'm not quite sure what.认为它与会话范围的 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.在我使用范围时,我使用了没有@ScopedProxy注释(或没有 aop 范围代理)的会话范围 bean,所以我真的很确定如何正确使用它。

Section 3.4.4.5 of the spring docs explains it pretty well: spring 文档的第 3.4.4.5 节很好地解释了它:

(please note that the following 'userPreferences' bean definition as it stands is incomplete): (请注意,以下“userPreferences”bean 定义不完整):

<!-- 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>

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'.从上面的配置可以明显看出,单例 bean 'userManager' 被注入了对 HTTP 会话范围的 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' bean 是一个单例......它会在每个容器中实例化一次并且它的依赖项(在这种情况下只有一个,'userPreferences' bean)也只会被注入(一次! )

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.这意味着“userManager”将(概念上)只对完全相同的“userPreferences”对象进行操作,即最初注入的对象。

This is not what you want when you inject a HTTP Session-scoped bean as a dependency into a collaborating object (typically).当您将 HTTP 会话范围的 bean 作为依赖项注入到协作对象中时(通常),这不是您想要的。 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 .相反,我们想要的是每个容器有一个“userManager”对象,然后,在 HTTP Session 的生命周期中,我们想要查看和使用特定于所述 HTTP Session 的“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.).相反,您需要的是注入某种对象,该对象公开与 UserPreferences 类完全相同的公共接口(理想情况下是一个 UserPreferences 实例的对象),并且该对象足够智能,能够离开并获取真正的 UserPreferences 对象从我们选择的任何底层范围机制(HTTP 请求、会话等)。 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”bean 中,它不会意识到它持有的 UserPreferences 引用是一个proxy

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.在我们的例子中,当 UserManager 实例调用依赖注入的 UserPreferences 对象上的一个方法时,它实际上会调用代理上的一个方法......然后代理将关闭并从中获取真正的 UserPreferences 对象(在这种情况下) HTTP 会话,并将方法调用委托给检索到的真实 UserPreferences 对象。

That is why you need the following, correct and complete, configuration when injecting request-, session-, and globalSession-scoped beans into collaborating objects:这就是为什么在将请求范围、会话范围和全局会话范围的 bean 注入协作对象时需要以下正确且完整的配置的原因:

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

After trying out various different options specified here and spring documentation, i have figured out for some reason Spring MVC, is wierdly autowiring controller when you use @Controller annotation and where you have more than one such controller in your webapp.在尝试了此处指定的各种不同选项和 spring 文档后,由于某种原因,我发现 Spring MVC 是一种奇怪的自动装配控制器,当您使用 @Controller 注释并且您的 web 应用程序中有多个这样的控制器时。 Modified the annotation to @RestController (value="UniqueControllerv1"), the issue is resolved.修改注解为@RestController (value="UniqueControllerv1"),问题解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM