Chapter 5. Scope: a fresh breath of state

 

This chapter covers:

  • Understanding what scope means
  • Understanding singleton and no scope
  • Applying practical scopes for the web

“Still this planet’s soil for noble deeds grants scope abounding.”

Johann Goethe

In one sentence, scope is a fixed duration of time or method calls in which an object exists. In other words, a scope is a context under which a given key refers to the same instance. Another way to look at this is to think of scope as the amount of time an object’s state persists. When the scope context ends, any objects bound under that scope are said to be out of scope and cannot be injected again in other instances.

State is important in any application. It is used to incrementally build up data or responsibility. State is also often used to track the context of certain processes, for instance, to track objects in the same database transaction.

In this chapter we’ll talk about some of the general-purpose scopes: singleton and no scope. These are scopes that are universally applicable in managing state. We’ll also look at managing state in specific kinds of applications, particularly the web. Managing user-specific state is a major part of scoping for the web, and this is what the request, session, flash, and conversation scopes provide. We’ll look at a couple of implementations of these with regard to Guice and Spring and how they may be applied in building practical web applications. First, we’ll take a primer on scopes.

5.1. What is scope?

5.2. The no scope (or default scope)

5.3. The singleton scope

5.4. Domain-specific scopes: the web

5.5. Summary

sitemap