Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pool objects in Spring?

I'm following this tutorial regarding how to pool objects in Spring. I've followed the instruction written on the tutorial but when I run my application, it always generates a new instance of the object. I'm expecting that since I'm pooling the objects, existing objects will be reuse. As such, no new instances should be created. Also, when I access of the getter method of the bean, a new instance of the bean is created again.

What could have I done wrong? Did I misunderstood the concept of pooling in Spring?

Below is my code:

Application Context: (This is just the body of my application context.)

<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>

The controller: (This is just the body of my method)

@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 line, A new SimpleBean... is printed again. I simply access the getter method. Why does it create a new instance of SimpleBean?

        return new ModelAndView("hello", "message", message);
    }catch(Exception e)
    {
        System.out.println("Error: " + e);
        e.printStackTrace();
        return new ModelAndView("hello", "message", "Error! " + e.getMessage());
    }
}

The bean I'm pooling:

package com.bean;

import java.util.Random;

public class SimpleBean
{
    int randomNum;
    String str;

    SimpleBean()
    {
        Random randomGenerator = new Random();
        randomNum = randomGenerator.nextInt(100);

        //I'm printing this line just to check if a instance of this bean is created.
        System.out.println("#####################A new SimpleBean was born: " + randomNum);

        str = "This is a string.";
    }

    public int getRandomNum()
    {
        return randomNum;
    }

    public void setRandomNum(int randomNum)
    {
        this.randomNum = randomNum;
    }

    public String getStr()
    {
        if (str == null)
            return "str is null";
        return str;
    }

    public void setStr(String str)
    {
        this.str = str;
    }
}

The body of my web.xml:

<display-name>Spring3MVC</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
like image 588
Arci Avatar asked Mar 18 '13 09:03

Arci


People also ask

What is object pool design pattern?

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object.

What is object pool in C++?

Object pool pattern is a software creational design pattern which is used in situations where the cost of initializing a class instance is very high. Basically, an Object pool is a container which contains some amount of objects.


1 Answers

On each request, you create a brand new Spring application context, then you get new object in a new applicatin context each action. so you should load your spring context use 'ContextLoaderListener' in web.xml.

Reference fragment in web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:spring/appContext.xml  classpath*:spring/appContext-security.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

See you code:

try
{
    System.out.println("Accessing Application Context");
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   ...

For more knowledge about Spring context loading, see MKyong 's tutorial or Spring reference

like image 189
Henry Leu Avatar answered Oct 20 '22 21:10

Henry Leu