Mixing Bean Scope

Learn what happens when the dependency of a singleton bean has prototype scope and how to get the correct output.

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.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.