In the Spring Framework, beans are defined with different scopes that govern their lifecycle and interaction with the container. The scope determines how many instances of a bean are created and how they are shared within an application.
Common Bean Scopes in Spring
Spring provides several bean scopes, and they are managed through the @Scope
annotation or XML configuration. Below are the commonly used scopes:
1. Singleton (Default Scope)
- This is the default scope in Spring.
- Ensures there is only one shared instance of the bean per Spring container.
- Suitable for stateless beans.
- Example:
@Component
public class MySingletonBean {
// Default scope is singleton
}
Or explicitly:
@Component
@Scope("singleton")
public class MySingletonBean {
}
2. Prototype
- A new instance of the bean is created every time it is requested.
- Useful for stateful, non-shared objects.
- Example:
@Component
@Scope("prototype")
public class MyPrototypeBean {
}
In XML:
<bean id="myBean" class="com.example.MyBean" scope="prototype" />
3. Request
- A new bean instance is created for every HTTP request and is specific to the lifecycle of that request.
- Used in web applications.
- Example:
@Component
@Scope("request")
public class MyRequestScopedBean {
}
4. Session
- A new bean instance is created for every HTTP session and remains valid for the session lifecycle.
- Mainly used in web applications for session-specific data.
- Example:
@Component
@Scope("session")
public class MySessionScopedBean {
}
5. Application
- A new bean instance is shared across the entire ServletContext (application-wide).
- Example:
@Component
@Scope("application")
public class MyApplicationScopedBean {
}
6. WebSocket
- A new instance is created for the lifecycle of a WebSocket session.
- Example:
@Component
@Scope("websocket")
public class MyWebSocketScopedBean {
}
Setting Bean Scope
- Using Annotations:
- Leverage the
@Scope
annotation along with Spring’s@Component
,@Service
,@Controller
, or@RestController
. - Example:
@Service @Scope("prototype") public class PrototypeService { }
- Leverage the
- Using XML Configuration:
- Define the scope inside the XML bean configuration.
- Example:
<bean id="myBean" class="com.example.MyService" scope="prototype" />
- Programmatically:
- Define the bean and its scope programmatically using Java-based configuration.
- Example:
@Configuration public class AppConfig { @Bean @Scope("prototype") public MyService myService() { return new MyService(); } }
Custom Scopes
You can define custom scopes in Spring by implementing the Scope
interface. This is typically used in special cases like tenant-based architectures.
How the Container Manages Scopes
In singleton scope, the container ensures that only one instance of the bean exists at a time. In prototype and other scopes, however, the container provides a fresh instance depending on the request but does not manage the full lifecycle, such as destroying the bean (in the case of prototype beans).
If you use scopes such as request
, session
, or websocket
, Spring’s WebApplicationContext
is responsible for managing the beans’ lifecycle.
Key Points:
- Singleton Scope is the default scope in Spring.
- For web applications, consider
request
,session
, orapplication
scopes for beans tied to web-specific functionalities. - For stateful beans, do not use Singleton scope unless thread safety is accounted for.
- Custom Scope definitions can extend the functionality of bean management.