Friday, June 24, 2016

Spring Bean Scoops

Spring support 5 types of bean scoops


  1. Singleton : singleton scope will return same object reference if you call multiple time to the bean from the IOC Container.
  2. Prototype  : It will return new object reference of the bean for every request we do with IOC  Container.
  3. Request : Return single bean instance per HTTP request.
  4. Session  : Return single bean instance per HTTP Session
  5. Global session : Returns single bean instance per HTTP Global Session.
  6. Application(V4.0): 
 Example of Singleton Scope: 
import com.javacontainer.service.customer;
public class CustomerService 
{
String message;
public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

Bean Configuration in Configuration xml file


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-2.5.xsd">

       
            class="com.javacontainer.service.customer.CustomerService" />
Calling the Bean
package com.javacontainer.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javacontainer.service.customer.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
    ApplicationContext context = 
    new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});

    CustomerService custA = (CustomerService)context.getBean("customerService");
    custA.setMessage("Message by custA");
    System.out.println("Message : " + custA.getMessage());
   
    //retrieve it again
    CustomerService custB = (CustomerService)context.getBean("customerService");
    System.out.println("Message : " + custB.getMessage());
    }
}

OutPut:

 Message : Message by custA
 Message : Message by custA


Example of Prototype Scope of Bean Configuration




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-2.5.xsd">

       
            class="com.javacontainer.service.customer.CustomerService"  scope="prototype"/>

OutPut

Message : Message by custA
Message : null

Bean Scope Configuration using Annotation :

package com.javacontainer.service.customer.CustomerService;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class CustomerService 
{
String message;
public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

To Read the annotation by the IOC Container we need to configure the element inside the spring configuration file :


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

       





No comments:

Post a Comment