Spring provides five built-in bean scopes. Two of them are available in all Spring applications, while the remaining three are available only in web-aware ApplicationContext implementations.
The five bean scopes are singleton, prototype, request, session, and application (previously referred to as global session in older versions of Spring).
![]() |
| Spring Bean Scopes |
- singleton
Example:
EmployeeService.javapackage com.adnblog.employee;
public class EmployeeService {
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Spring bean configuration file : spring-employee.xml (if scope is not declared, default it should be singleton)
<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-2.5.xsd">
<bean id="employeeService"
class="com.adnblog.employee.EmployeeService" />
</beans>
Spring code - Main Program
package com.adnblog;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.adnblog.employee.EmployeeService;
public class MainClass {
public static void main( String[] args ) {
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"spring-employee.xml"});
EmployeeService empService = (EmployeeService)context.getBean("employeeService");
empService.setMessage("empService Message");
System.out.println("Message : " + empService.getMessage());
//trying to create second instance
EmployeeService empService1 = (EmployeeService)context.getBean("employeeService");
System.out.println("Message : " + empService1.getMessage());
}
}
Output : Message : empService Message
Message : empService Message
- prototype
To use the prototype scope, set the bean's scope to prototype in the bean configuration, as shown below:
<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-2.5.xsd">
<bean id="employeeService"
class="com.adnblog.employee.EmployeeService" scope="prototype"/>
</beans>
Run the main program, you will get output
Output : Message : empService Message
Message : null
- request
This scope is available only in a web-aware ApplicationContext, such as WebApplicationContext.
- session
This scope is available only in a web-aware ApplicationContext, such as WebApplicationContext.
- global-session
The global session scope is specific to Portlet-based applications. In a Portlet container, an application consists of multiple portlets. Each portlet has its own session, but if you want to share a bean instance across all portlets within the same application, you can use the global session scope.In Servlet-based applications, the global session scope has no special behavior and functions the same as the session scope. It is worth noting that the global session scope is primarily intended for Portlet environments and is rarely used in modern Spring Boot applications.

No comments:
Post a Comment