Injecting lists
The following listing shows the Bean Configuration in the XML file for injecting lists:
Listing. Injection of list in XML configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<bean id="beanExample" class="com.mybook.common.BeanExample"> <property name="listProperty"> <list> <value>10</value> <ref bean="otherBean" /> </list> </property> </bean> <bean id="otherBean"> <property name="propertyString" value="myString" /> <property name="propertyInteger" value="32" /> </bean> |
Injecting Other Collection Types
The topics in this section explain injection of other collection types using XML configuration. Examples are included for collection types such as injecting properties Set and Map.
Injecting properties
Bean Configuration in the XML file for injecting properties is given below:
Listing. Injection of properties in XML configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<bean id="beanExample" class="com.mybook.common.BeanExample"> <property name=" myProperties "> <props> <prop key="prop.applicationName">cargoLoyalty</prop> <prop key="prop.applicationVersion">3.0.0.0</prop> </props> </property> </bean> |
Injecting Set
In the below example, the bean otherBean is local to the main bean beanExample. In the example for injecting list, the bean namely otherBean is global and can be accessed by any other user.
Bean Configuration in the XML file for injecting set is given below:
Listing. Injection of set in XML configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<bean id="beanExample" class="com.mybook.common.BeanExample"> <property name="setProperty"> <set> <value>10</value> <ref bean="otherBean" /> <bean class="com.mybook.common.OtherBean"> <property name="propertyString" value="myString" /> <property name="propertyInteger" value="32" /> </bean> </set> </property> </bean> |
Injecting Map
Bean Configuration in the XML file for injecting map is given below:
Listing. Injection of map in XML configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<bean id="beanExample" class="com.mybook.common.BeanExample"> <property name="maps"> <map> <entry key="key1" value="1" /> <entry key="key2" value-ref="secondBean" /> <entry key="key3"> <bean class="com.mybook.common.OtherBean"> <property name="propertyString" value="myString" /> <property name="propertyInteger" value="32" /> </bean> </entry> </map> </property> </bean> <bean id="secondBean" class="com.mybook.common.SecondBean"> <property name="propertyString" value="myString" /> <property name="propertyInteger" value="32" /> </bean> |
Page Visitors: 4528
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi there,
There is one mistake repeated several times here. The value of the propertyInteger is being set using “ref” instead of “value” and you may want to remove the spaces before and after some of the values/properties to avoid others new to the framework copying and pasting and getting tripped up.
Apart from that, thanks for making the material available for free.
Sure will do the necessary changes. I wrote the material using MS Word and after that when i copied, these spaces automatically came in.
Thanks for pointing it out.
Regards
Tomcy John