How to do it...

This recipe will be using both ch02-xml and the ch02-jc project in declaring which beans are considered Singleton and Prototype. We will explore and identify the effects of applying either of the two scopes to the container:

  1. Open the project ch02-xml and locate the XML definition file in the ch02-xmlsrcmainjava directory. Let us convert empRec1 to a Singleton bean by setting the value of its scope attribute to Singleton:
<bean id="empRec1" class="org.packt.starter.ioc.model.Employee" 
   scope="Singleton" /> 
By default, all loaded beans in the container are Singleton.
  1. Convert the bean empRec2 to a Prototype one with its scope attribute set to Prototype:
<bean id="empRec2" class="org.packt.starter.ioc.model.Employee" 
   scope="Prototype"> 
   // refer to sources 
</bean>
  1. Let us focus now on project ch02-jc and open its context BeanConfig definition. Convert the scope of empRec1 to a Singleton bean by applying the Spring-based annotation @Scope with a value Singleton:
@Bean(name="empRec1") 
@Scope("Singleton") 
public Employee getEmpRecord1(){ 
      Employee empRec1 = new Employee(); 
      return empRec1; 
} 
By default, all loaded beans, even without the @Scope annotation, are Singleton.
  1. Make the scope of empRec2 a Prototype by applying the same annotation @Scope with the value Prototype:
@Bean(name="empRec2") 
@Scope("Prototype") 
public Employee getEmpRecord2(){ 
      // refer to sources 
      return empRec2; 
} 
  1. Lastly, create a test class TestScopes for each project that will be used to fetch the preceding objects many times. Observe the number of instances the container creates per scoped bean. Use the object's hashCode() method to determine the identity of each instance:
public class TestScopes { 
 
   public static void main(String[] args) { 
       AnnotationConfigApplicationContext context = 
new AnnotationConfigApplicationContext(); 
       context.register(BeanConfig.class); 
      
       System.out.println("application context loaded."); 
           
       context.refresh(); 
       System.out.println("*********The empRec1 bean ******"); 
       Employee empRec1A = (Employee) context.getBean("empRec1"); 
       System.out.println("instance A: " + empRec1A.hashCode()); 
       Employee empRec1B = (Employee) context.getBean("empRec1"); 
       System.out.println("instance B: " +empRec1B.hashCode()); 
          
       System.out.println("*********The empRec2 bean **********"); 
       Employee empRec2A = (Employee) context.getBean("empRec2"); 
       System.out.println("instance A: " + empRec2A.hashCode()); 
       Employee empRec2B = (Employee) context.getBean("empRec2"); 
       System.out.println("instance B: " + empRec2B.hashCode()); 
          
       context.registerShutdownHook(); 
   } 
} 
  1. Running the test class will give us a result that will prove that a Singleton bean is only created once in its lifetime during the entire application execution while Prototype beans are always instantiated every time the application fetches them from the container:
*********The empRec1 bean *************** 
instance A: 1691875296 
instance B: 1691875296 
*********The empRec2 bean *************** 
instance A: 667346055 
instance B: 1225197672 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.179