9 Using the Spring web scopes

 

This chapter covers

  • Using the Spring web scopes
  • Implementing a simple login functionality for a web app
  • Redirecting from one page to another in a web app

In chapter 5, we discussed Spring bean scopes. You learned that Spring manages a bean’s life cycle differently depending on how you declare the bean in the Spring context. In this chapter, we’ll add some new ways Spring manages the beans in the context. You’ll learn Spring has custom ways to manage instances for web apps by using the HTTP request as a point of reference. Spring is pretty cool, isn’t it?

In any Spring app, you can choose to declare a bean as one of the following:

  • Singleton—The default bean scope in Spring, for which the framework uniquely identifies each instance with a name in the context
  • Prototype—The bean scope in Spring, for which the framework only manages the type and creates a new instance of that class every time someone requests it (directly from the context or through wiring or auto-wiring).

In this chapter, you’ll learn that in web apps you can use other bean scopes that are relevant only to web applications. We call them web scopes:

9.1 Using the request scope in a Spring web app

9.2 Using the session scope in a Spring web app

9.3 Using the application scope in a Spring web app

Summary