1
votes

I have a bean scope defined as a prototype which refers a another bean whose scope is singleton. Now from the spring documentation, I know

singleton

This scopes the bean definition to a single instance per Spring IoC container (default).

prototype

This scopes a single bean definition to have any number of object instances.

Below is my code :

<bean id="employee" class="com.sample.beans.Employee" scope="prototype">
    <property name="id" value="1"/>
    <property name="name" value="employee_1"/>
    <property name="department">
        <ref bean="department"/>
    </property>
</bean>
<bean id="department" class="com.sample.beans.Department" scope="singleton">
    <property name="id" value="1"/>
    <property name="name" value="hardware"/> 
</bean>

So does it mean that I am forcing Spring to produce a new Employee bean instance each time one is needed, and Department bean is single per IOC container. But internally when employee calls department bean, does it create a new one or it shares the existing singleton bean which has been created earlier.

1
All instances of employee bean will refer to a single department bean. Should work fine. - iluxa

1 Answers

0
votes

It should share the previous bean Department