It's valid for the same instance to be returned twice, in the same thread. It's hard to test this kind of pooling behaviour, as either the same or diff instances may be validly returned. What _would_ be invalid would be blocking in different threads.
You really need to write a multi-threaded test to verify the behaviour, although there's no indication of a problem.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am test the instance pooling, I follow the reference 10.5.2., but when ctx.getBean twice, it g
ave me same instance, please help!<br>
<bean id="businessObjectTarget" class="com.ping.MyBusinessObject"
singleton="false">
</bean>
<bean id="poolTargetSource"
class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName"><value>businessObjectTarget</value></property>
<property name="maxSize"><value>25</value></property>
</bean>
<bean id="businessObject"
class="org.springframework.aop.framework.ProxyFactoryBean"
>
<property name="targetSource"><ref local="poolTargetSource"/></property>
<!--<property name="interceptorNames"><value>myInterceptor</value></property>-->
</bean>
<br>
package com.ping;
public class MyBusinessObject {
public String getSomething() {
return this.toString();
}
}
test code:<BR>
MyBusinessObject mybusiness = (MyBusinessObject) ctx.getBean("businessObject");
MyBusinessObject mybusiness1 =(MyBusinessObject) ctx.getBean("businessObject");
System.out.println(mybusiness1.getSomething());
System.out.println(mybusiness.getSomething());
<br>
result:
com.ping.MyBusinessObject@c9d92c
com.ping.MyBusinessObject@c9d92c
It's valid for the same instance to be returned twice, in the same thread. It's hard to test this kind of pooling behaviour, as either the same or diff instances may be validly returned. What _would_ be invalid would be blocking in different threads.
You really need to write a multi-threaded test to verify the behaviour, although there's no indication of a problem.