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

Posted on

Introduction

This article will focus on about how to use the prototype scope in the Java Spring Application using NetBeans IDE. Actually, this article has a connection in term of the content with the previous article. That previous article exist in this link with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’. In other words, this article is using the base project exist in that article as an example. The usage of the ‘prototype’ value in the scope parameter in the Spring Bean definition is very useful.

The ‘prototype’  is one of the value which is defining 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 ‘prototype’ will have a new instance upon a new available request.

Using Prototype Scope in Java Spring Application Using NetBeans IDE

  1. First of all, just open the Spring Configuration file. In this example, it is a file with the name of ‘spring-config.xml’. The file itself exist in a folder of ‘src/main/resources’. The following line is the definition of the parameter ‘scope’ to the Spring Bean definition with the value of ‘prototype’ :

    <?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="prototype">
           </bean>
    </beans>

    Just remove all the properties or the attributes in the Spring Configuration file. The remaining is only the Spring Bean definition as in the above example.

  2. Next step, in order to prove it, modify the main Java file source 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. For the source modification, just change 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. Next, execute the program above, the following output will appear :

    As in the above output, the last object with the name of ’employee2′ cannot print the value of the ‘id’ property. That is because the term or the value ‘prototype’ for the scope of the Spring Bean in the Spring Configuration file. It means that it will create a totally different object or instance without having to share any of their attribute or property respectively. So, two different instance which is ’employee1′ and ’employee2′ will have a different and separate value of its own attribute or property.

Leave a Reply