user496949
user496949

Reputation: 86135

Can I use spring to achieve this following goal?

Bean A depends on another Bean B. (Bean B is a property of Bean A).

I want Bean B sometime have objects and sometimes be null.

Upvotes: 0

Views: 52

Answers (1)

roghughe
roghughe

Reputation: 169

The simple answer is Yes. In terms of Spring 2.x XML (and this will work in 3.x):

<bean id="A" class="my.bean.A">
    <property name="property_B">
        <ref local="B"/>
    </property>
</bean>

<bean id="B" class="my.bean.B"/>

You can build on this to expand 'B' so that it has its own properties:

<bean id="B" class="my.bean.B">
    <property name="property_C">
        <ref local="C"/>
    </property>
</bean>

You can make B null with respect to A by changing you XML so that B is not injected into A

<bean id="A" class="my.bean.A"/>

Upvotes: 2

Related Questions