Spring 属性注入(int ,String, List, Map, Set, String[])
1.JavaBean
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
private int intValue;
private String strValue;
private List listValue;
private Map mapValue;
private Set<String> setValue;
private String[] arrayValue;
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public String getStrValue() {
return strValue;
}
public void setStrValue(String strValue) {
this.strValue = strValue;
}
public List getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
public Map getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
public Set getSetValue() {
return setValue;
}
public void setSetValue(Set setValue) {
this.setValue = setValue;
}
public String[] getArrayValue() {
return arrayValue;
}
public void setArrayValue(String[] arrayValue) {
this.arrayValue = arrayValue;
}
}
2.Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="TestBean" value="11" />
<!-- 注入String数据 -->
<property name="strValue">
<value>Tom</value>
</property>
<!-- 注入List数据 -->
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<!-- 注入Set数据 -->
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
<value>set4</value>
</set>
</property>
<!-- 注入数组数据 -->
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
<value>array3</value>
<value>array4</value>
</list>
</property>
<!-- 注入map数据 -->
<property name="mapValue">
<map>
<entry key="k1" value="map1" />
<entry key="k2" value="map2" />
<entry key="k3" value="map3" />
<entry key="k4" value="map4" />
</map>
</property>
</bean>
</beans>
3.测试bean
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.BeanFactory;
public class TestSpringDI {
private static BeanFactory factory;
public static void main(String[] args) {
factory = new ClassPathXmlApplicationContext(
"conf/spring/applicationContext1Test.xml");
testBeanDemo();
}
public static void testBeanDemo() {
Test test = (Test) factory.getBean("TestBean");
System.out.println("beanDemo.intValue=" + test.getIntValue());
System.out.println("beanDemo.strValue=" + test.getStrValue());
List listValue = test.getListValue();
System.out.println("beanDemo.listValue=");
if (listValue != null) {
for (Iterator it = listValue.iterator(); it.hasNext();) {
System.out.println((String) it.next());
}
}
System.out.println("beanDemo.setValue=");
Set setValue = test.getSetValue();
if (setValue != null) {
for (Iterator it = setValue.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
System.out.println("beanDemo.arrayValue=");
String[] arrayValue = test.getArrayValue();
if (arrayValue != null) {
for (int i = 0; i < arrayValue.length; i++) {
System.out.println(arrayValue[i]);
}
}
System.out.println("beanDemo.mapValue=");
Map mapValue = test.getMapValue();
if (mapValue != null) {
Set<Map.Entry> set = mapValue.entrySet();
for (Iterator it = set.iterator(); it.hasNext();) {
Map.Entry entry = (Entry) it.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
}
4.输出结果
beanDemo.intValue=11
beanDemo.strValue=Tom
beanDemo.listValue=
list1
list2
list3
beanDemo.setValue=
set1
set2
set3
set4
beanDemo.arrayValue=
array1
array2
array3
array4
beanDemo.mapValue=
k1 = map1
k2 = map2
k3 = map3
k4 = map4