Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between Singleton and Prototype bean

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 292
    Comment on it

    Singleton Bean Scope

    The singleton bean is termed as a stateless bean i.e. it doesn't carry any state with it. Only one instance of the object is created which is defined by this type of bean. One instance is created per container.All the other beans refer to this single instance of the bean. The scope of any bean is singleton by default but still if you want a single instance always , then you can define the scope property in the configuration file.

    Syntax:

    <!-- Defining singleton scope -->
    <bean id="[bean-id]" class="[Class-name]" scope="singleton">
        <!-- Other configuration-->
    </bean>
    

    Example:

    Welcome.java

    public class Welcome {
       private String message;
    
       public void setMessage(String message){
          this.message  = message;
       }
       public void getMessage(){
          System.out.println("The message for you is: " + message);
       }
    }
    

    MainDemo.java

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainDemo {
       public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    
          Welcome ob1 = (Welcome) context.getBean("welcome");
    
          ob1.setMessage("Welcome in first object");
          ob1.getMessage();
    
          Welcome ob2 = (Welcome) context.getBean("welcome");
          ob2.getMessage();
       }
    }
    

    Beans.xml

    <?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="welcome" class="Welcome" scope="singleton">
       </bean>
    </beans>
    

    When you try running this, the output will be as:

    The message for you is: Welcome in first object
    The message for you is: Welcome in first object

    Prototype Bean Scope

    The prototype bean is termed as a statefull bean i.e. it carries state with it. i.e the object members. You can create as many instances of this bean and with every new request associated with the bean a new instance is created. You can define the scope in the configuration file as prototype.

    Syntax:

    <!-- Defining singleton scope -->
    <bean id="[bean-id]" class="[Class-name]" scope="prototype">
        <!-- Other configuration-->
    </bean>
    

    Example:

    Welcome.java

    public class Welcome {
       private String message;
    
       public void setMessage(String message){
          this.message  = message;
       }
       public void getMessage(){
          System.out.println("The message for you is: " + message);
       }
    }
    

    MainDemo.java

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainDemo {
       public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    
          Welcome ob1 = (Welcome) context.getBean("welcome");
    
          ob1.setMessage("Welcome in first object");
          ob1.getMessage();
    
          Welcome ob2 = (Welcome) context.getBean("welcome");
          ob2.getMessage();
       }
    }
    

    Beans.xml

    <?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="welcome" class="Welcome" scope="prototype">
       </bean>
    </beans>
    

    When you try running this, the output will be as:
    The message for you is: Welcome in first object
    null

    This is because we pass the parameters for the second instance created for the bean.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: