A singleton-scoped bean has a dependency on a prototype-scoped bean. What is the potential problem with this design?

Java interview question for Advanced practice.

Answer

The singleton bean receives a new instance of the prototype bean only once upon its own creation, defeating the purpose of the prototype scope.

Explanation

This is a classic Spring issue known as the 'scoped bean injection problem'. The singleton bean is created only once, and its dependencies are injected at that time. Therefore, it gets a single instance of the prototype-scoped bean, and this same instance is reused on every call to process(). This negates the 'prototype' behavior, where a new instance is expected each time it's needed.

Related Questions