Spring | Injecting a Prototype bean in a Singleton Bean | Problem and Solution | Simple Programming

Publicado el: 08 junio 2020
en el canal de: Simple Programming
29,949
585

In this video, let us discuss a very common interview question nowadays.
What happens when you inject a prototype bean inside a singleton bean?

Now if you ask question like can you tell me about the different scopes in spring, everyone will tell singleton, prototype, request, session, application, websocket etc and thy will even given definitions of these scope

but you slightly tweak the question and ask them what exactly happens when you have that scenario… And being an interviewer myself, 9 in 10 candidates will not give the right answer. In some cases if the candidate is able to tell guess the answer, they will not be able to give a solution to the problem.

alright, so we all know the default scope in spring in singleton. that is the container creates a single instance of that bean, and for any number of requests spring is going to give the same object, which is actually cached in the container.

and what is prototype - every time it is requested, a different instance is returned from the container.

so what exactly happens when you inject two different scopes together. i.e prototype into singleton

both beans will be initialized only once, at the startup.. prototype will loose its actual behavioral property.

lets take a look at this using an example and what we will do also address this problem using different methodologies.

We will be fixing this problem with,
1. ApplicationContext
2. Method Injection (@Lookup)
3. Provider (JSR component)
4. ObjectFactory

#prototype #singleton #springboot


En esta página del sitio puede ver el video en línea Spring | Injecting a Prototype bean in a Singleton Bean | Problem and Solution | Simple Programming de Duración hora minuto segunda en buena calidad , que subió el usuario Simple Programming 08 junio 2020, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 29,949 veces y le gustó 585 a los espectadores. Disfruta viendo!