AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

Spring Bean Scopes

In this new post, we will delve into some Spring theory and explore the different types of Spring Bean scopes.

The scope of a bean defines its lifecycle and visibility within the context in which we use it. Based on this definition, we can identify the following types:

  • singleton
  • prototype
  • request
  • session
  • application
  • websocket

Next, we will define each of these scopes.

Singleton Scope in Spring

Singleton Scope is the default scope in Spring. It works in a way that all requests made to that bean always return the same object, which is cached.

To create a singleton bean, we can either not specify anything during the bean creation, which implies that it is of singleton scope, or define it in the following ways:

For example, for a car object:

@Getter
@Setter
public class Car {
    private String model;

}

Now that we have defined the object, let’s define the bean and create it as a singleton:

@Bean
@Scope("singleton")
public Car car() {
    return new Car();
}

Alternatively, we can define it as follows:

@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)

Scope Prototype in Spring

In the case where our bean is of type Prototype, the Spring Inversion of Control container will create a new instance of the object every time a request is made for that bean. Typically, we use the Prototype scope in Spring when we want to maintain state.

Similar to the Singleton scope, we can define it as follows:

@Bean
@Scope("prototype")
public Car car() {
    return new Car();
}

And we can also define it like this:

@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)

Scope Request in Spring

Request Scope is a type of Scope Bean in Spring that creates a new bean for each HTTP request made. This type of bean can be used for authentication and authorization purposes.

This type of bean is defined differently from the previous ones. We define it as follows:

@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public Car car() {
    return new Car();
}

In the previous bean definition, we added the proxyMode attribute because it will be necessary when the application context is instantiated and there is no active request.

Alternatively, we can define it as:

@Bean
@RequestScope
public Car car() {
    return new Car();
}

This attribute is used to handle scenarios where the bean is accessed outside the scope of an active request. It allows for the creation of a proxy that dynamically provides the appropriate instance of the bean based on the current request context.

Scope Session in Spring

The Session Scope, unlike the Request Scope, creates a single bean for the HTTP session.

The way to create this type of bean is very similar to the Request Scope:

@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public Car car() {
    return new Car();
}

We can also create it using the specific annotation available for this type of bean:

@Bean
@SessionScope
public Car car() {
    return new Car();
}

Scope Application in Spring

Application Scope Bean creates a bean for the entire lifecycle of the ServletContext. In other words, it creates an instance that lasts throughout the runtime of the application.

This scope is similar to the Singleton Scope but with a difference: Singleton has a scope limited to a single ApplicationContext, while Application is a singleton scope specific to the ServletContext.

@Bean
@Scope(
  value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public Car car() {
    return new Car();
}

Alternatively, we can use the specific annotation for this scope:

@Bean
@ApplicationScope
public Car car() {
    return new Car();
}

Both approaches create a bean that has a lifespan tied to the ServletContext and behaves similar to a singleton instance, but scoped to the entire application.

Scope WebSocket in Spring

The WebSocket Scope is a special type in which the created bean lasts for the duration of the WebSocket session.

This scope is used to create applications with WebSocket functionality, allowing bidirectional message exchange between the client and server. The WebSocket Scope will live as long as the WebSocket session is active.

@Bean
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Car car() {
    return new Car();
}

Conclusion

In this post, we have explored the different types of Spring Bean Scopes, enabling us to better understand how they work and apply them in various scenarios.

If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!

Leave a Reply

Your email address will not be published. Required fields are marked *