Spring - Bean Scopes with Annotations

Spring - Bean Scopes with Annotations

We have previously discussed the Spring bean scopes using an XML-based configuration. In the XML-based configuration, we have defined the beans and its scope inside the bean-config file (applicationContext).

 Here, we are going to use the @Scope annotation to define the scope of a bean. The @Scope annotation can be used in two ways: as a type-level annotation with @Component or as a method-level annotation with @Bean annotation. The scope defines the lifecycle of an instance. The org.springframework.context.annotation package contains the @Scope annotation.

As we have discussed, there are five scopes in the Spring framework, which are listed below:

  • singleton
  • prototype
  • request
  • session
  • global session

The request, session, and global session are the scopes used with web-aware apps.

Following code shows how to use singleton scope:

@Component
@Scope("singleton")
public class Cricket_Coach implements Coach {
….
} 

Following code shows how to use prototype scope:

@Component
@Scope("prototype")
public class Cricket_Coach implements Coach {
….
}

Example of singleton bean scope

Now, we are going to create an example of a singleton bean scope. In the following example, we have created an interface Coach that contains an unimplemented method. The Cricket_Coach class implements the Coach interface and its unimplemented method.

The singleton scope is the default scope. If we don’t use the @Scope annotation to define the singleton scope, by default Spring will consider it as the singleton scope.

Following are the steps used to create an example of singleton bean scope:

Coach.java

public interface Coach {
public String training();
} 

Cricket_Coach.java

import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;
 @Component
 @Scope("singleton")
 public class Cricket_Coach implements Coach {
 public Cricket_Coach() {
 System.out.println( "default constructor of Cricket_Coach" ) ;
}
public String training() { 
return "10 rounds of the ground daily..!!" ;
}
} 

In the above class, we have defined the @Scope annotation with the singleton scope just below the @Component annotation.

applicationContext.xml

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

App.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieving the beans from the Spring 
Coach coach1 = appcontext.getBean("cricket_Coach" , Coach.class);
Coach coach2 = appcontext.getBean("cricket_Coach", Coach.class);
// check if both the objects are same
boolean result = (coach1 == coach2);
System.out.println("  Check  whether pointing to the same object : " + result);
System.out.println("Memory location for coach1 :  " + coach1);
System.out.println("Memory location for coach1 :  " + coach2);
appcontext.close(); 
}
} 

Output

The following output shows that both the objects are at the same memory location.

output shows that both the objects

Now, we are going to create an example of a prototype bean scope.

Example of prototype bean scope

In the following example, we are going to create an interface Coach which contains an unimplemented method. The Cricket_Coach class implements the Coach interface and its unimplemented methods.

Following are the steps used to create an example of a prototype bean scope:

Coach.java

public interface Coach {
public String training();
} 

Cricket_Coach.java

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class Cricket_Coach implements Coach {
public Cricket_Coach() {
System.out.println( "default constructor of Cricket_Coach" ) ;
}
public String training() { 
return "10 rounds of the ground daily..!!" ;
}
} 

applicationContext.xml

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

App.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class App 
 {
public static void main( String[] args )
{
ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieves beans from the Spring  
Coach coach1 = appcontext.getBean("cricket_Coach" , Coach.class);
Coach coach2 = appcontext.getBean("cricket_Coach", Coach.class);
// check if both the objects are same or not 
boolean result = (coach1 == coach2);

System.out.println("Check whether pointing to the same object : " + result);
System.out.println("Memory location for coach1 : " + coach1);
System.out.println("Memory location for coach1 : " + coach2);
appcontext.close(); 
}
} 

Output

The following output shows that both the objects are at different memory locations.

output shows that both the objects are at different memory locations