Mixing Bean Scope
Explore how Spring handles beans with different scopes, specifically when a singleton bean depends on prototype beans. Learn techniques like proxy usage and the @Lookup annotation to ensure correct creation and injection of prototype-scoped beans. Understand the impact of bean scopes on object lifecycle and memory management in Spring applications.
We'll cover the following...
Singleton bean with prototype dependency
In this lesson, we will discuss an interesting problem of mixing bean scopes. Sometimes, a bean has singleton scope but its dependency has prototype scope. An example is the content-based filter which recommends movies based on item-to-item similarity. Our basic implementation of the content-based filter compares different movies and assigns a similarity score. Hence, Movie is a dependency of the ContentBasedFilter class.
The ContentBasedFilter bean has singleton scope because we need only one instance of the filter. However, the Movie bean has prototype scope because we need more than one objects of this class.
For the code example shown in this lesson, we have created a sub-package called lesson9 inside the package io.datajek.spring.basics.movierecommendersystem.
The package contains MovieRecommenderSystemApplication.java, Filter.java, and ...