Spring bean scopes

In the previous section, we learned various DI patterns, and saw how to create beans in a Spring container. We also learned various DI configuration such as XML, Java, and annotation. In this section, we will learn more details about bean life and scope available in a Spring container. The Spring container allows us to control the bean at configuration level. This is a very flexible way to define object scope at configuration level, instead of at the Java-class level. In Spring, the bean is controlled through the scope attribute that defines what kind of object has to be created and returned. When you describe <bean>, you have the option of defining scope for that bean. The bean scope describes the life cycle and visibility of that bean in the context of where it used. In this section, we will see the different types of bean scope in Spring Framework.

Here is an example of defining bean scope at XML-based configuration:

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

<!-- Here scope is not defined, it assume default value 'singleton'.
It creates only one instance per spring IOC. -->
<bean id="customerService" class="com.packt.springhighperformance.ch2.bankingapp.service.Impl.CustomerServiceImpl" />

<!-- Here scope is prototype, it creates and returns bankingService object for every call-->
<bean id="bankingService" class="com.packt.springhighperformance.ch2.bankingapp.model.BankingService" scope="prototype">

<bean id="accountService" class="com.packt.springhighperformance.ch2.bankingapp.model.AccountService" scope="singleton">

</beans>

 Here is an example of defining bean scope using the @Scope annotation:

@Configuration
public class AppConfig {

@Bean
@Scope("singleton")
public CustomerService showCustomerAccountBalance() {
return new CustomerServiceImpl();

}
}

We can also use a constant instead of the string value in the following manner:

@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Following are bean scopes available in Spring Framework:

  • The singleton bean scope: As we saw in the previous example of bean configuration in XML-based, if scope is not defined in the configuration, then the Spring container considers scope as singleton. The Spring IoC container creates exactly only one single instance of the object, even if there are multiple references to a bean. Spring stores all singleton bean instances in a cache, and all subsequent requests of that named bean return the cached object. This is needed to understand that the Spring bean singleton scope is a little different from the typical singleton design pattern that we are using in Java. In Spring singleton, scope creates one object of that bean per one Spring container, meaning if there are multiple Spring containers in single JVM then multiple instances of that bean will be created.
  • The prototype bean scope: When scope is set to prototype, the Spring IoC container creates a new bean instance of object every time a bean is requested. Prototype-scoped beans are mostly used for stateful beans.
As a rule, use prototype scope for all stateful beans, and the singleton scope for stateless beans.
  • The request bean scope: The request bean scope is only available in a web-aware application context. The request scope creates a bean instance for each HTTP request. The bean is discarded as soon as the request processing is done.
  • The session bean scope: The session bean scope is only available in a web-aware application context. The session scope creates a bean instance for every HTTP session.
  • The application bean scope: The application bean scope is only available in a web-aware application context. The application scope creates a bean instance per web application.
..................Content has been hidden....................

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