我正在按照本教程关于如何在Spring中池化对象.我按照教程中的说明进行操作,但是当我运行我的应用程序时,它总是会生成一个新的对象实例.我期待,因为我正在汇集对象,现有的对象将被重用.因此,不应创建新实例.此外,当我访问bean的getter方法时,会再次创建bean的新实例.
我怎么可能做错了?我是否误解了Spring中汇集的概念?
以下是我的代码:
应用程序上下文:(这只是我的应用程序上下文的主体.)
<bean id="simpleBeanTarget" class="com.bean.SimpleBean" scope="prototype">
</bean>
<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
<property name="targetBeanName" value="simpleBeanTarget" />
<property name="maxSize" value="2" />
</bean>
<bean id="simpleBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource" ref="poolTargetSource" />
</bean>
Run Code Online (Sandbox Code Playgroud)
控制器:(这只是我方法的主体)
@RequestMapping("/hello")
public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response)
{
String message = "Hello World, Spring 3.";
try
{
System.out.println("Accessing Application Context");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Getting Bean");
SimpleBean simpleBean = (SimpleBean) context.getBean("simpleBean");
//A new SimpleBean... is printed here.
System.out.println("Displaying Hello World: " + simpleBean.getRandomNum());
//After this …Run Code Online (Sandbox Code Playgroud)