Thứ Ba, 17 tháng 7, 2012

Spring Bean Scope Singleton by Default

While studying difference between GOF Singleton pattern and
SpringFramework Singleton scope for the Bean/POJO, I thought of
writing this blog.

As you might be knowing that default scope for the bean within
Spring container is singleton, and while using Spring's ApplicationContext
and BeanFactory we will be getting a single instance to work with..

Thus we may be using the bean instance from the BeanFactory of singleton
in nature, but it is quite different from the GOF Singleton Pattern.
 
As GOF Singleton Pattern will be looking at an instance that is single
or unique per class and per classloader level.

But SpringFramework Bean singleton scope is of single in nature
but within SpringFramework container boundary/level.

So for a particular Spring ApplicationContext and BeanFactory, there
could be a single bean instance for a particular bean with a unique id.

So let us explore more on this with the help of following example,
where I will be creating a single application context and BeanFactory using
ClassPathXmlApplicationContext api from SpringFramework by passing this
example test.xml configuration file as argument parameter.

Then I am retrieving a Bean instance form the BeanFactory using a bean id
and then setting value for the instance variable of this Bean instance.

On retrieving another Bean instance for the second time for the same bean
id as that of bean id used in the first case.

Just to imagine in case Spring container manages singleton scope for this
Bean, then I should expect the value set in the first case,
to be retained in the second case as well.

import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.context.support.*;

public class Testclient{
  public Testclient() {
    try{
        ApplicationContext appContext =
            new ClassPathXmlApplicationContext("test.xml");
        BeanFactory factory = appContext;
        //Retrieving bean from the bean factory.
        //Bean id as "one"
        One one1 = (One)factory.getBean("one");
        one1.setName("test name");

        //Bean id as "one"
        One one2 = (One)factory.getBean("one");
        System.out.println("Name from the singleton bean :"+one2.getName());
    } catch(Exception ex) {
        ex.printStackTrace();
    }
  }
  public static void main(String args[]) {
      new Testclient();
  }
}
Above bold red color text shows the steps/code used to retrieve Bean instance from Spring container and then some random name value is set to this instance. While the bold blue colored text above, shows the way another bean instance is retrieved from Spring container using the same bean id. The value from the second bean instance will be able to return the value that is already set in the earlier step. This shows that both these instances are the same, thus can be treated as singleton from Spring's singleton scope. This example is then slightly modified to include another instance of SpringFramework's ApplicationContext and BeanFactory instances again, and then the same ways some random value for name field is set in first case, and then the value is retrieved from the another returned Bean instance from Spring Container. But now there is a difference being observed, the returned value is null, not the original value that is set in first case.
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.context.support.*;

public class Testclient{
  public Testclient() {
    try{
        ApplicationContext appContext =
            new ClassPathXmlApplicationContext("test.xml");
        BeanFactory factory = appContext;
        //Retrieving bean from the bean factory.
        One one1 = (One)factory.getBean("one");
        one1.setName("test name");

        ApplicationContext appContextNew =
            new ClassPathXmlApplicationContext("test.xml");
        BeanFactory factoryNew = appContextNew;
        One one2 = (One)factoryNew.getBean("one");
        System.out.println("Name from the singleton bean :"+one2.getName());
    } catch(Exception ex) {
        ex.printStackTrace();
    }
  }
  public static void main(String args[]) {
      new Testclient();
  }
}
The above modified code uses two different Spring Context and so we have two different bean instances, while one context taking the setter value and the other context returning null value. Code for the Bean in this example, is as follows:
public class One
{
  private String name;
  public void setName(String arg) {
   name = arg;
  }
  public String getName() {
   return name;
  }
}
and the Spring configuration file as follows:



  
  

 

Không có nhận xét nào: