liveBook · Manning

Application scope

As explained in Spring Start Here, Second Edition

Application scope is a Spring bean scope in which Spring creates one bean instance for the whole application and shares it across requests.

Application scope gives a Spring web application one bean instance shared across requests from all clients. It is close to singleton scope, but the Spring context cannot contain multiple instances of the same type. section 9.0 section 9.3

Counting login attempts

An application-scoped LoginCountService can store a shared login-attempt count. A request-scoped LoginProcessor increments it; a controller passes the count to a view for display. section 9.3

Mutable shared attributes can cause concurrency problems and require synchronization. Application-scoped data also stays in memory while the app runs. Avoid this scope for mutable data when possible; a database is usually a better place for shared data. section 9.3 section 9.5

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.

See how one application-scoped bean instance is shared across requests from all clients. section 9.3

How Spring Start Here, Second Edition covers it

How deep it goes
Defines the scope, shows a login-counter example, and discusses risks without detailing lifecycle internals.
Examples
Demonstrates a shared login-attempt counter incremented during login and displayed on the main page.
What it assumes
Assumes familiarity with Spring beans, HTTP requests, and singleton scope rather than teaching those foundations.
What you can do after
Evaluate whether application scope suits shared data by weighing concurrency risks against a database alternative.
How it connects
Compares application scope with singleton, request scope, and session scope, and recommends a database for shared data.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.