Creating request- and session-scoped beans
Chapter 2, Learning Dependency Injection (DI), discussed a recipe about configuring the lifespan of a bean inside the ApplicationContext
container based on fetching or getBean()
. These are the long-lived singleton and prototype beans. Now, we will discuss configuring the lifespan or scope of some beans which are bounded within MVC web transactions. This recipe will discuss creating short-lived beans that only last during request dispatch and session handling.
Getting started
Open the same ch03
project we have created previously and perform the following steps.
How to do it...
To create and differentiate session- and request-based beans, follow these steps:
- This recipe needs some custom models that can be injected into the container: either request-scoped or session-scoped beans. First, let us create a model
SalaryGrade
in theorg.packt.dissect.mvc.model.data
package. This model must be injected as a@Bean
into theApplicationContext
through the annotation...