Thursday, 21 April 2016

Bean scope scenario : Prototyped inner bean of a Singleton bean


Scenario

<bean id="singletonBean" class="...">
    <property name="instanceBean" ref="instanceBean" />
</bean>
<bean id="instanceBean" class="..." scope="prototype" />

When I call : 
singletonBean = context.getBean("singletonBean");  // Get main bean
// ...some code...
singletonBean = context.getBean("singletonBean");  // Get it again

Would property instanceBean of singletonBean be initialized again or it would just use already created singleton ?


Answer

It would just use already created singleton.
A prototyped inner bean of a singleton won't be recreated each time you get the singleton from context.
The singleton and all is references are created one for all.

But context.getBean("instanceBean");  would give you a new since scope is 'prototype'.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.