Application Scope in Spring Web Applications
Overview
In a Spring web application, the application scope is a bean scope where a single instance of a bean is shared across the entire application. This means that all client requests, regardless of their origin, access the same instance of the bean. This scope is akin to a singleton, but it is specifically designed for web applications where HTTP requests are the reference point for the bean’s lifecycle.
Definition
Application scope refers to a bean scope in a Spring web application where there is only one instance of the bean for the entire application. Every request from any client can access this single instance.
Understanding Application Scope
The application scope is illustrated in Figure 9.14, which shows that the instance of an application-scoped bean is shared by all HTTP requests from all clients. The Spring context provides only one instance of the bean’s type, which is used by anyone who needs it.
Figure 9.14 Understanding the application scope in a Spring web app. The instance of an application-scoped bean is shared by all the HTTP requests from all clients. The Spring context provides only one instance of the bean’s type, used by anyone who needs it.
Example: LoginCountService
To illustrate the application scope, consider the LoginCountService
class, which counts login attempts across all users. This service is defined as an application-scoped bean, meaning it maintains a single count for all login attempts in the application.
Listing 9.12 The LoginCountService class counts the login attempts
@Service
@ApplicationScope #1
public class LoginCountService {
private int count;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
#1 The @ApplicationScope annotation changes the scope of this bean to the application scope.
In this example, the @ApplicationScope
annotation is used to specify that the LoginCountService
bean should be application-scoped. This means that the count
attribute is shared across all HTTP requests, and any increment operation affects the global count.
Usage in Controllers
The LoginProcessor
can auto-wire the LoginCountService
bean and call the increment()
method for any new login attempt. Additionally, the count value can be sent from a controller to a view for display purposes.
Listing 9.14 Sending the count value from controller to be displayed on the main page
@Controller
public class MainController {
// Omitted code
@GetMapping("/main")
public String home(
@RequestParam(required = false) String logout,
Model model
) {
if (logout != null) {
loggedUserManagementService.setUsername(null);
}
String username = loggedUserManagementService.getUsername();
int count = loginCountService.getCount(); #1
if (username == null) {
return "redirect:/";
}
model.addAttribute("username" , username);
model.addAttribute("loginCount", count); #2
return "main.html";
}
}
#1 Gets the count from the application-scoped bean
#2 Sends the count value to the view
In this listing, the MainController
retrieves the login count from the LoginCountService
and adds it to the model, which is then used to display the count on the main page.
Considerations
While the application scope can be useful for sharing data across the entire application, it is not recommended for production applications due to concurrency issues. Since all requests share the same instance, mutable attributes can lead to inconsistent states. It is advisable to use immutable attributes or a persistence layer, such as a database, to manage shared data safely.
FAQ (Frequently asked questions)
What is the application scope in a Spring web app?
Why is the application scope not recommended for production apps?
What alternatives are suggested instead of using application scope in production apps?
Can you give an example of using application scope in a Spring web app?
How does the application scope work in a Spring web app?
What does the @ApplicationScope annotation do in Spring?
What is the purpose of the LoginCountService class in the context of application scope?
What is the definition of application scope in a Spring web application?
How many instances of a bean exist in application scope?