Spring Dependency Injection: Injecting Collections, Arrays, Maps, Properties, Bean References and Advanced Configurations
This article explains how Spring's XML configuration can inject simple values, collections, arrays, maps, Properties, bean references, internal beans, null values, and object‑graph navigation, and it provides concise syntax examples and test code for each case.
Spring can inject not only simple type data but also collections (Collection, Set, List), arrays, maps, and java.util.Properties. The following sections demonstrate how to configure each type and how to reference other beans.
1. Injecting Collection Types
List injection : Use the <list> tag in the bean definition.
package cn.javass.spring.chapter3.bean;
import java.util.List;
public class ListTestBean {
private List<String> values;
public List<String> getValues() { return values; }
public void setValues(List<String> values) { this.values = values; }
}Bean definition (resources/chapter3/listInject.xml):
<bean id="listBean" class="cn.javass.spring.chapter3.bean.ListTestBean">
<property name="values">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>Test code:
@Test
public void testListInject() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("chapter3/listInject.xml");
ListTestBean listBean = beanFactory.getBean("listBean", ListTestBean.class);
System.out.println(listBean.getValues().size());
Assert.assertEquals(3, listBean.getValues().size());
}Set injection : Use the <set> tag, configuration is analogous to <list> .
<bean id="setBean" class="cn.javass.spring.chapter3.bean.SetTestBean">
<property name="values">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
</bean>Collection injection (the base type of List and Set) can be performed with either <list> or <set> tags.
2. Injecting Array Types
Use the <array> tag; its attributes such as value-type and merge work like those of <list> .
3. Injecting Map (Dictionary) Types
Use the <map> tag. The key-type and value-type attributes specify the types of keys and values. Keys are defined with <key> and values with <value> (or <ref> for bean references).
<bean id="mapBean" class="cn.javass.spring.chapter3.bean.MapTestBean">
<property name="values">
<map key-type="java.lang.String" value-type="java.lang.String">
<entry key="one" value="1"/>
<entry key="two" value="2"/>
</map>
</property>
</bean>4. Injecting Properties
Use the <props> tag; both keys and values must be strings.
<bean id="propsBean" class="cn.javass.spring.chapter3.bean.PropsTestBean">
<property name="props">
<props>
<prop key="host">localhost</prop>
<prop key="port">8080</prop>
</props>
</property>
</bean>5. Referencing Other Beans
Bean references can be injected via constructor arguments or setter properties. The ref attribute or a nested <ref> element specifies the target bean ID.
Constructor injection example:
<bean id="bean1" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">
<constructor-arg index="0" ref="helloApi"/>
</bean>Setter injection example:
<bean id="bean2" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">
<property name="helloApi"><ref bean="helloApi"/></property>
</bean>Test code for bean references:
@Test
public void testBeanInject() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("chapter3/beanInject.xml");
HelloApi bean1 = beanFactory.getBean("bean1", HelloApi.class);
bean1.sayHello();
HelloApi bean2 = beanFactory.getBean("bean2", HelloApi.class);
bean2.sayHello();
}6. Advanced Reference Styles
<ref local="..."/> references a bean defined in the same configuration file; <ref parent="..."/> references a bean in the parent container.
Local reference example (throws an error if the bean does not exist):
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: ... No ID/IDREF binding for IDREF ‘helloApi’.Configuration using local and parent :
<bean id="bean1" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">
<constructor-arg index="0"><ref local="helloApi"/></constructor-arg>
</bean>
<bean id="bean2" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">
<property name="helloApi"><ref parent="helloApi"/></property>
</bean>7. Internal Bean Definition
An internal bean is defined inside a <property> or <constructor-arg> element using the <bean> tag. It receives an anonymous identifier and is not visible to other beans.
<bean id="bean" class="cn.javass.spring.chapter3.bean.HelloApiDecorator">
<property name="helloApi">
<bean class="cn.javass.spring.chapter2.helloworld.HelloImpl"/>
</property>
</bean>Test code:
@Test
public void testInnerBeanInject() {
ApplicationContext context = new ClassPathXmlApplicationContext("chapter3/innerBeanInject.xml");
HelloApi bean = context.getBean("bean", HelloApi.class);
bean.sayHello();
}8. Injecting Null Values
Use the <null/> tag; a literal "null" would be treated as a string.
<property name="optional"><null/></property>9. Object‑Graph Navigation Injection
Spring supports navigation such as navigationB.navigationC or indexed access like list[0] , array[0] , map[key] . All intermediate objects must be non‑null.
<bean id="a" class="cn.javass.spring.chapter3.bean.NavigationA">
<property name="navigationB" ref="b"/>
<property name="navigationB.navigationC" ref="c"/>
<property name="navigationB.list[0]" ref="c"/>
<property name="navigationB.map[key]" ref="c"/>
<property name="navigationB.properties[0]" ref="c"/>
<property name="navigationB.array[0]" ref="c"/>
</bean>Test code verifies that each navigation path resolves to the same NavigationC instance, printing five "===navigation c" lines.
10. Configuration Shortcuts
Constructor arguments and properties can be written in a compact form:
Constant value: <constructor-arg index="0" value="constant"/>
Reference: <constructor-arg index="0" ref="beanId"/>
Property constant: <property name="msg" value="constant"/>
Property reference: <property name="msg" ref="beanId"/>
Collections ( <list> , <set> ) and arrays have no short form, while maps can use <entry key="k" value="v"/> or <entry key-ref="kBean" value-ref="vBean"/> .
11. Using the p Namespace
By declaring xmlns:p="http://www.springframework.org/schema/p" , setter injection can be simplified:
<bean id="bean1" class="java.lang.String" p:value="test"/>
<bean id="idrefBean2" class="cn.javass.spring.chapter3.bean.IdRefTestBean" p:id-ref="bean1"/>These shortcuts are equivalent to the full <property> syntax.
Source: Zhang Kaitao (http://sishuok.com/forum/blogPost/list/2447.html)
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.