chapter nine

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

Spring manages a bean’s life cycle differently depending on how you declare the bean in the Spring context. Now, let’s add some new ways Spring manages the beans in the context. Did you know that 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?

Up to this point, we discussed the two main application scopes: singleton and prototype. Above this, you’ll learn that in web apps, you can use bean scopes that are relevant only to web applications. We call them web scopes, and there are three types that we’ll discuss.

First, the request scope, where Spring creates an instance of the bean class for every HTTP request; the instance exists only for that specific HTTP request. Then we have the session scope, where Spring creates an instance and keeps the instance in the server’s memory for the full HTTP sessions—Spring links the instance in the context within the client’s session. Last, we have the application scope. In the application scope, the instance is unique in the app’s context and is available when the app is running.

We begin our examination with the request scope.

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

9.4 Questions

9.5 Summary