Spring Bean Scopes – Examples


There are 5 spring bean scopes as below:

1. Singleton: 

If scope is set to singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.

If we not set any scope to bean then spring container will set to the default scope and default scope of the bean is always singleton. use ‘singleton’ to set the bean scope to Singleton.

2. Prototype

If scope is set to prototype, the Spring IoC container creates new bean instance of the object every time a request for that specific bean is made.

Use ‘prototype’ word during spring configuration to set the bean scope to Proto Type.

As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.

3. Request:

This scopes a bean definition to an HTTP Request and its only valid in the context of a web-aware Spring ApplicationContext.

Use the ‘request’ keyword to set the bean scope to HttpRequest during spring bean configuration.

4. Session:

This scopes a bean definition to an HTTP Session and its only valid in the context of a web-aware Spring ApplicationContext.

Use the ‘session’ keyword to set the bean scope to Http Session during spring bean configuration.

5. Global Session

This scopes a bean definition to a Global HTTP Session and its only valid in the context of a web-aware Spring ApplicationContext.

Use the ‘global-session’ keyword to set the bean scope to Global Http Session during spring bean configuration.

Configuration of these scopes can be done as below:

Define the above mentioned scopes in beans.xml file along with bean declaration.

<!– A bean definition with singleton scope –>
<bean id=”…” class=”…” scope=”singleton”>
<!– collaborators and configuration for this bean go here –>
</bean>

Can also be mention the bean scope using annotation:

@Configuration
public class BeanJavaConfiguration {
@Bean
@Scope(“prototype”)
public Appleapp() {
return new Apple();
}
}

 

Different Types of Spring Containers


There are two distinct types of spring containers as mentioned below:

1. BeanFactory Container:

  • This is the simplest container providing basic support for Dependency Injection and defined by the org.springframework.beans.factory.BeanFactory interface. Related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, can provide callbacks which can be configured for the different phases of the Spring Bean’s life cycle.
  • BeanFactory, by default lazy loads the beans, it creates the bean only when the getBean() method is called.

2. ApplicationContext Container:

  • This container adds more enterprise-specific functionalities such as ability to resolve textual messages from a properties file and ability to publish application events to the interested event listeners. This container is defined by org.springframework.context.ApplicationContext interface.
  • ApplicationContext loads all Spring Beans upon start-up unlike BeanFactory.

ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory. BeanFactory can be used for light weight applications like mobile devices or applet based applications where data volume and speed is significant.

 

Is it mandatory to have the “id” property in Bean/ApplicationContext Xml


Typical Bean Configuration XML file is given below.

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

<bean class="com.malliktalksjava.service.EmployeeService">
    <property name="empRepository" ref="employeeDAO"/>
</bean>

<bean id="employeeDAO" class="com.malliktalksjava.dao.EmployeeDao"/>

</beans>

From the above config, EmployeeService Bean is Anonymous because no id is supplied explicitly. Hence, Spring Container generates a unique id for that bean. It uses the fully qualified class name and appends a number to it.

However, if you want to refer that bean by name, through the use of the ref element you must provide a name.

Right way of doing is shown using second Bean, its been declared with employeeDAO id attribute in order to be referenced by the empRepository property of the first Bean.