Saturday, January 11, 2014

Spring Course: V16 Inner Beans

as we saw in all our previous examples, when you reference a bean you do like this

<bean id="jungle" class="com.caveofprogramming.spring.test.Jungle">
<property name="largest" ref="lion"></property>
</bean>

<bean id="lion" class="com.caveofprogramming.spring.test.Animal">
<property name="name" value="Igor"></property>
<property name="type" value="lion"></property>
</bean>

so we defined a lion bean, and we are referring to this bean in the jungle bean.

the lion bean can be referred to in any beans, however sometimes you want the bean to be used only in one location, in this case you can define that bean as an inner bean as follows:

<bean id="jungle" class="com.caveofprogramming.spring.test.Jungle">
<property name="largest" >
              <bean id="lion" class="com.caveofprogramming.spring.test.Animal">
          <property name="name" value="Igor"></property>
       <property name="type" value="lion"></property>
             </bean>
        </property>
</bean>

as you can see the lion bean is defined now inside <property> which makes it an inner bean, lion bean is used just to set this property and you cannot refer to it from any other place.

No comments:

Post a Comment