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

Veröffentlicht am: 08 Juni 2020
auf dem Kanal: 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


Auf dieser Seite können Sie das Online-Video Spring | Injecting a Prototype bean in a Singleton Bean | Problem and Solution | Simple Programming mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Simple Programming 08 Juni 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 29,949 Mal angesehen und es wurde von 585 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!