Java 如何实际使用Spring池对象?
我正在研究这里描述的spring池的一个示例 我在这里读过一个非常类似的问题,但仍然没有答案 假设我在SpringConfig文件中定义了以下bean,那么我实际上如何使用池对象Java 如何实际使用Spring池对象?,java,spring,pool,Java,Spring,Pool,我正在研究这里描述的spring池的一个示例 我在这里读过一个非常类似的问题,但仍然没有答案 假设我在SpringConfig文件中定义了以下bean,那么我实际上如何使用池对象 <bean id="businessObjectTarget" class="com.mycompany.MyBusinessObject" scope="prototype"> ... properties omitted </bean> <bean id="poolTar
<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"/>
</bean>
我实际上应该如何使用池对象
谢谢
PS-我现在更不确定它是否能工作。我做了以下更改并提交了两个线程。第一个有效,第二个错误指示同时使用:
<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName" value="businessObjectTarget" />
<property name="maxSize" value="1" />
<property name="whenExhaustedActionName" value="WHEN_EXHAUSTED_FAIL" />
</bean>
我很怀疑。就像你现在这样。对于您的代码来说,使用池对象和常规对象(应该是这样)没有区别。我仍然感到困惑。资源注释不能用于方法级变量,因此池对象需要位于类级。由于拥有对象本身是一个单实例,因此businessObject变量将同时被多个线程访问。这仍然是个问题-不?不,不是。您将获得处理池的
MyBusinessObject
的代理。
<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName" value="businessObjectTarget" />
<property name="maxSize" value="1" />
<property name="whenExhaustedActionName" value="WHEN_EXHAUSTED_FAIL" />
</bean>