为单例bean创建的引用会话bean/原型bean的实例数 [英] How many instances created for singleton bean referring to a session bean/prototype bean

查看:151
本文介绍了为单例bean创建的引用会话bean/原型bean的实例数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定使用Spring Framework时在以下提到的场景中将创建的实例数量:

I have a doubt about the number of instances that will be created in the scenario mentioned below, when Spring Framework is used:

bean的配置是这样的

<bean id="a" class="A">
    <property name="b" ref="b"/>
</bean>

<bean id="b" class="B" scope="session"/> or

<bean id="b" class="B" scope="prototype"/>

默认情况下,bean"a"具有singleton scope.因此,有一个单例bean引用了具有会话范围或原型范围的bean.

By default, bean "a" has singleton scope. So there is a singleton bean with a reference to a bean with session scope or prototype scope.

在这种情况下,如果对应用程序有2个同时请求,那么将创建多少个A实例,以及多少个B实例?

In this case, if there are 2 simultaneous requests to the application, then how many instances of A will be created and how many instances of B will be created?

如果任何人都可以解释它的工作原理,将会有很大的帮助.

It will be of great help if anyone can explain how this works.

谢谢, Divya

推荐答案

单例作用域

当一个bean是singleton时,将仅管理该bean的一个共享实例,并且对所有具有与该bean定义匹配的id或id的bean的请求将导致Spring返回一个特定的bean实例.容器.

When a bean is a singleton, only one shared instance of the bean will be managed, and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned by the Spring container.

换句话说,当您定义bean定义并将其范围限定为singleton时,Spring IoC container将由该bean定义定义create exactly one instance of the object.该单个实例将存储在此类单例bean的缓存中,并且对该命名bean的所有后续请求和引用将导致返回缓存的对象.

To put it another way, when you define a bean definition and it is scoped as a singleton, then the Spring IoC container will create exactly one instance of the object defined by that bean definition. This single instance will be stored in a cache of such singleton beans, and all subsequent requests and references for that named bean will result in the cached object being returned.

会话范围

使用上述bean定义后,Spring容器将为lifetime of a single HTTP Session创建bean的全新实例.

With the above bean definition in place, the Spring container will create a brand new instance of the bean , for the lifetime of a single HTTP Session.

根据Spring框架参考,在需要将"lives longer"(在这种情况下为单粒豆)的类与寿命相对较短的另一类(会话)一起注入的情况下,需要采用不同的方法. -scoped bean).对于原型&的方法是不同的.但单例作用域.

According to Spring framework reference, a different approach needs to be followed in cases where a class which "lives longer"(singleton bean in this case) needs to be injected with another class having a comparatively shorter life-span(session-scoped bean). The approach is different for prototype & singleton scope though.

在您的XML中,我们想要的是singletonBean实例应该只实例化一次,并且应该与sessionBean一起注入.但是由于sessionBean是会话作用域的(这意味着应该为每个会话重新实例化),因此配置是不明确的(因为在实例化时设置了依赖项,并且会话作用域的值也可以在以后更改).

In your XML, what we want is that the singletonBean instance should be instantiated only once, and it should be injected with sessionBean. But since sessionBean is session-scoped(which means it should be re-instantiated for every session), the configuration is ambiguous(as the dependencies are set at instantiation time and the session scoped value can change later also).

因此,与其注入该类,不如注入一个代理,该代理公开与sessionBean完全相同的公共接口.容器将此代理对象注入到singletonBean bean中,而后者不知道此sessionBean引用是代理.通过在sessionBean中写入以下标记来指定其内容:

So instead of injecting with that class, its injected with a proxy that exposes the exact same public interface as sessionBean. The container injects this proxy object into the singletonBean bean, which is unaware that this sessionBean reference is a proxy. Its specified by writing this tag in the sessionBean:

<aop:scoped-proxy/>

XML配置:

<bean name="singletonBean" class="somepkg.SingletonBean">
<property name="someProperty" ref="sessionBean"/>
</bean>

<bean name="sessionBean" class="somepkg.SessionBean" scope="session">
<aop:scoped-proxy/>
</bean>

singletonBean实例在注入依赖项的sessionBean对象上调用方法时,实际上是在代理上调用方法.然后,代理从HTTP会话中获取真实的sessionBean对象(在这种情况下),并将方法调用委托给检索到的真实的sessionBean对象.

When a singletonBean instance invokes a method on the dependency-injected sessionBean object, it actually is invoking a method on the proxy. The proxy then fetches the real sessionBean object from (in this case) the HTTP Session, and delegates the method invocation onto the retrieved real sessionBean object.

还请参考 以获取更多信息.

Alse please refer this for more info.

具有原型Bean依赖性的单个Bean

查找方法注入

当使用对prototype beans具有依赖关系的singleton-scoped bean时,请注意,依赖关系在实例化时已解决.因此,如果将prototype-scoped Bean依赖项注入到单例范围的Bean中,则将实例化新的原型Bean,然后将依赖项注入到该Singleton Bean中.原型实例是唯一提供给单例作用域的bean的实例.

When you use singleton-scoped beans with dependencies on prototype beans, be aware that dependencies are resolved at instantiation time. Thus if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to the singleton-scoped bean.

但是,假设您希望单例作用域的bean在运行时重复获取原型作用域的bean的新实例.您不能将依赖于作用域的原型bean依赖注入到您的单例bean中,因为当Spring容器实例化单例bean并解析并注入其依赖项时,该注入仅发生一次.

However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into your singleton bean, because that injection occurs only once, when the Spring container is instantiating the singleton bean and resolving and injecting its dependencies.

<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
  <!-- inject dependencies here as required -->
</bean>

<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
  <lookup-method name="createCommand" bean="command"/>
</bean>

Lookup method注入是容器对override methods on container受管bean的能力,以返回容器中另一个命名bean的查找结果.如上一节所述,lookup通常包含prototype bean. Spring框架通过使用从CGLIB library生成字节码来动态生成覆盖该方法的子类来实现此方法注入.

Lookup method injection is the ability of the container to override methods on container managed beans, to return the lookup result for another named bean in the container. The lookup typically involves a prototype bean as in the scenario described in the preceding section. The Spring Framework implements this method injection by using bytecode generation from the CGLIB library to generate dynamically a subclass that overrides the method.

引用查找方法注射.

关注以获取更多详细示例和信息.

Follow for more detailed example and information.

这篇关于为单例bean创建的引用会话bean/原型bean的实例数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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