Chapter 9: Using the Spring web scopes
Introduces Spring’s three web bean scopes by comparing how long their instances are available.
After this chapter you will be able to
- Web scope instance lifetimes. Distinguish request, session, and application scopes by when Spring creates a bean instance and how long it remains available.
- Request-scoped bean lifetime. Explain when Spring creates a request-scoped bean instance and which requests can use it.
- Login scope choice. Justify using a request-scoped LoginProcessor for credentials while leaving the LoginController at its default singleton scope.
- Login request processing flow. Trace how the login form, controller, and request-scoped LoginProcessor produce a result message, and describe how the processor bean is declared.
- Session-scoped bean lifetime. Explain how a session-scoped bean shares data across requests from the same client, and distinguish its lifetime from a request-scoped bean’s lifetime.
- Session-based login state. Describe how the example records a successful login in a session-scoped bean while keeping the login processor request-scoped.
- Protected page and logout flow. Trace how the example permits access to the main page after login, redirects unauthenticated users, and logs a user out.
- Application Scope Sharing. Explain that all client requests share an application-scoped bean and that the scope is similar to a singleton, with the distinction about instances of the same type stated in the section.
- Shared State Production Risks. Justify the section's advice to avoid application-scoped beans in production by describing their singleton-like concurrency problems and the alternatives it recommends.
What it builds on
- Using the singleton bean scope (chapter 5) · Introduces singleton as an application bean scope.
- Using the prototype bean scope (chapter 5) · Introduces prototype as an application bean scope.
- Using the GET and POST HTTP methods (chapter 8) · Using GET and POST requests in a web app.
- Using stereotype annotations to create beans (chapter 2) · Registering beans with stereotype annotations.
- Implementing web apps with a dynamic view (chapter 8) · Controllers and views in a Spring web app.
Section by section
Introduction
Introduces Spring’s three web bean scopes by comparing how long their instances are available.
- Web scope instance lifetimes: Distinguish request, session, and application scopes by when Spring creates a bean instance and how long it remains available.
Why it matters: These different lifetimes help you identify which web scope fits the time a bean needs to remain available.
Key terms: web scopes, request scope, session scope, application scope
9.1 Using the request scope in a Spring web app
Explains how request-scoped beans give each HTTP request its own bean instance, using a login example to show when and how to use the scope.
- Request-scoped bean lifetime: Explain when Spring creates a request-scoped bean instance and which requests can use it.
Why it matters: This is the distinction that makes request scope useful for data that belongs to one request. - Login scope choice: Justify using a request-scoped LoginProcessor for credentials while leaving the LoginController at its default singleton scope.
Why it matters: Scope choice depends on what data a bean holds, not simply on whether it participates in a web request. - Login request processing flow: Trace how the login form, controller, and request-scoped LoginProcessor produce a result message, and describe how the processor bean is declared.
Why it matters: The example connects request scope to the concrete path from submitted credentials to a displayed response. - Login example security limits: Distinguish the chapter's illustrative login logic from an appropriate production authentication approach.
Why it matters: It prevents a scope demonstration from being mistaken for recommended security practice.
Key terms: request-scoped bean, HTTP request, instance, LoginController, LoginProcessor, credentials, singleton, @RequestScoped, @Component, message
9.2 Using the session scope in a Spring web app
This section explains how session-scoped beans retain data across one client’s requests and uses a stored username to implement login, protected-page access, and logout.
- Session-scoped bean lifetime: Explain how a session-scoped bean shares data across requests from the same client, and distinguish its lifetime from a request-scoped bean’s lifetime.
Why it matters: This distinction tells you which scope can retain information as a client moves between pages. - Session-based login state: Describe how the example records a successful login in a session-scoped bean while keeping the login processor request-scoped.
Why it matters: Keeping only the enduring username in session scope lets later requests recognize the logged-in client. - Protected page and logout flow: Trace how the example permits access to the main page after login, redirects unauthenticated users, and logs a user out.
Why it matters: The stored username gives the controller a way to decide whether to display the protected page or send the client to login.
Key terms: session-scoped bean, HTTP session, request-scoped bean, @SessionScope, LoggedUserManagementService, LoginProcessor, username, MainController, /main, redirect:
9.3 Using the application scope in a Spring web app
This section explains how application-scoped beans share state across clients, illustrates that behavior with a login counter, and cautions against using the scope in production.
- Application Scope Sharing: Explain that all client requests share an application-scoped bean and that the scope is similar to a singleton, with the distinction about instances of the same type stated in the section.
Why it matters: The sharing boundary determines who sees state held in the bean. - Shared State Production Risks: Justify the section's advice to avoid application-scoped beans in production by describing their singleton-like concurrency problems and the alternatives it recommends.
Why it matters: Sharing a mutable bean across clients makes concurrency a concern, even though it can seem convenient for application-wide data. - Shared Login Counter Example: Trace how the example counts login attempts across all users and displays the total on the main page.
Why it matters: The example makes the effect of sharing one bean across users visible as a single total.
Key terms: application scope, application-scoped bean, singleton, concurrency problems, immutable attributes, singleton bean, persistence layer, database, LoginCountService, LoginProcessor
9.4 Questions
This section provides review questions about request, session, and application scopes and when to use a database instead.
9.5 Summary
Summarizes how request, session, and application bean scopes differ in lifetime and sharing, and the design risks of keeping data in longer-lived scopes.
Concepts explained in this chapter
- request scopeA request-scoped bean is a Spring-managed bean with a distinct instance for each HTTP request.
- session scopeA session-scoped bean is a Spring-managed bean whose instance is shared across requests in the same HTTP session.
- @RequestScoped annotationThe @RequestScoped annotation marks a Spring bean as having request scope.
- credentialA credential is information, such as a username and password, used to verify a user's identity.
- application scopeApplication scope is a Spring bean scope in which Spring creates one bean instance for the whole application and shares it across requests.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
