Harshil
Harshil

Reputation: 473

scope singleton in one bean and prototype other dependent bean

I need to understand how Spring will behave on the below situation. Suppose I have two beans in my application-context.xml

Case 1:

<bean id="user" class="com.test.User" >
    <constructor-arg ref="department"/>
</bean>
<bean id="department" class="com.test.Department" scope="protoType"></bean>

Case 2:

<bean id="user" class="com.test.User" scope="protoType">
    <constructor-arg ref="department"/>
</bean>

<bean id="department" class="com.test.Department"></bean>

Upvotes: 1

Views: 357

Answers (1)

WeMakeSoftware
WeMakeSoftware

Reputation: 9162

First case:

User bean will be singleton. At the time of context creation it will get a new instance of the Department bean. Every time the Department bean is injected / accessed from application context a new instance of Department will be created. It won't be the same as the one previously injected into User bean.

Second case:

Every time the User bean will be injected / requested from the context it will be a newly created bean with the reference to the singleton Department bean.

Upvotes: 1

Related Questions