如何使用Pooled Spring bean而不是Singleton? [英] How to use Pooled Spring beans instead of Singleton ones?

查看:122
本文介绍了如何使用Pooled Spring bean而不是Singleton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了提高效率,我有兴趣限制同时使用Spring应用程序环境的bean的线程数(我不希望在我的中处理一个无限制线程数

For efficiency reasons, I am interested in limiting the number of threads that simultaneously uses the beans of the Spring application context (I don't want an unlimited number of threads proccessing in my limited memory).

我发现这里(spring文档)通过以下方式将Bean集中在EJB样式中来实现此目的:

I have found here (spring documentation) a way to achieve this by pooling the beans in a EJB style, by doing the following:


  • 将目标bean声明为范围prototype。

  • 声明一个将提供有限数量的池提供者汇总的目标实例。

  • 声明一个ProxyFactoryBean,该功能对我来说不清楚。

这是bean的声明:

<bean id="businessObjectTarget" class="com.mycompany.MyBusinessObject" 
    scope="prototype">
  ... properties omitted
</bean>

<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
  <property name="targetBeanName" value="businessObjectTarget"/>
  <property name="maxSize" value="25"/>
</bean>

<bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="targetSource" ref="poolTargetSource"/>
  <property name="interceptorNames" value="myInterceptor"/>
</bean>

我的问题是当我将声明另一个bean使用businessObjectTarget的池化实例时我该怎么办?我的意思是,当我尝试做这样的事情:

My problem is that when I will declare another bean to use pooled instances of the "businessObjectTarget", how should I do it? I mean, when i try to do something like this:

<bean id="clientBean" class="com.mycompany.ClientOfTheBusinessObject">
  <property name="businessObject" ref="WHAT TO PUT HERE???"/>
</bean>

ref的值应该是什么?

What should be the value of the "ref" ??

推荐答案

您不能使用属性来获取原型的实例。

一个选项是使用查找方法(请参阅第3.3.7.1节

另一个选项来获取你的bean在代码中:使您的 com.mycompany.ClientOfTheBusinessObject 实现 ApplicationContextAware 接口,然后调用 context.getBean(clientBean)

You cannot use properties to get instances of prototypes.
One option is to use the lookup methods (see chapter 3.3.7.1)
Another option to get your bean in code: make your com.mycompany.ClientOfTheBusinessObject to implement the ApplicationContextAware interface and then call context.getBean("clientBean")

这篇关于如何使用Pooled Spring bean而不是Singleton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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