How to Use Singleton Scope in Java Spring Application Using NetBeans IDE

Posted on

Introduction

Yet another article focusing on how to set a scope in the Java Spring Application using NetBeans IDE. But in this article, it is focusing on a different type of value of the scope. In this article, the context of the scope value is ‘singleton’. So, this article will point out about how to use the singleton scope using a sample of Java Spring Application. The other article which is using ‘prototype’ scope exist as an article with the title of ‘How to Use Prototype Scope in Java Spring Application Using NetBeans IDE’. For further reference, that article exist in this link as a comparison.

In general, the usage of the ‘singleton’ value in the scope parameter in the Spring Bean definition has a different concept from the ‘prototype’ value. The application as an example for further demonstration in this article exist in the article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’. In order to read it in detail, just visit it in this link.

So, using the base application in that article, just define the ‘singleton’ value as the scope of the lifecycle of a Spring Bean. It is also one of the value that is possible for the scope parameter definition in the ApplicationContext file. Basically, a Spring Bean with the definition scope with the value of ‘singleton’ share properties among multiple instance.

Using Singleton Scope in Java Spring Application Using NetBeans IDE

  1. The first step is just take a look at the Spring Configuration file. As for an example, there is a file with the name of ‘spring-config.xml’. In this context, the file is located in a folder of ‘src/main/resources’. Furthermore, the following line is the definition of the parameter ‘scope’ to the Spring Bean definition with the value of ‘singleton’ :

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
           <bean id="employeeBean" class="com.test.spring.Employee" scope="singleton">
           </bean>
    </beans>

    Remove all the properties or the attributes which exist inside the above definition of the Java Spring Bean. It is for the sake of the sample using just the above Spring Bean definition.

  2. Following into the next step, use the Java Spring Bean with the name of ‘Employee’ as in the following lines :

    public class Employee {
            private String id; 
            public void Employee(){
    
            }
            public void setId(String id) {
               this.id = id;
            }
    
            public void identifyID(){
               System.out.println("I am an "+this.id+" !");
            }
    }
    
  3. Next, modify the main source of the Java program as follows :

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext
    
    public class SpringMain {
           public static void main(String[] args){ 
                  ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
                  Employee employee1 = (Employee) context.getBean("employeeBean");
                  employee1.setId("Employee of the Company");
                  employee1.identifyID();
                  Employee employee2 = (Employee) context.getBean("employeeBean");
                  employee2.identifyID();
           }
    }
    
  4. Last but not least, just execute the main program above. The following output as in the image below will appear :

    How to Use Singleton Scope in Java Spring Application Using NetBeans IDE
    How to Use Singleton Scope in Java Spring Application Using NetBeans IDE

    In the image display above, the last object with the name of ’employee2′ will print the same value of the ‘id’ property with the object with the name ’employee1′. The reason is because of the term or the value ‘singleton’ for the scope of the Spring Bean in the Spring Configuration file. It does not even care whether both of them are totally different object or instance.  Because those objects will have to share their attribute or property respectively. In other words, two different instance which is ’employee1′ and ’employee2′ will share and have exactly the same value of their attribute or property.

Leave a Reply