集合注入
本文主要内容是演示如何进行list,set,map,props等集合注入,包括普通类型和对象
package com.picc.spring.depencyinjection;
public class SomeOne {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "SomeOne{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
package com.picc.spring.depencyinjection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class SomeBean {
private String sex;
private List list;
private Set set;
private Map map;
private Properties prop;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
@Override
public String toString() {
return "SomeBean{" +
"sex='" + sex + '\'' +
", list=" + list +
", set=" + set +
", map=" + map +
", prop=" + prop +
'}';
}
}
public class springBean {
private static ClassPathXmlApplicationContext classPath;
@Test
public void testlist(){
SomeBean apple = classPath.getBean("someBean", SomeBean.class);
System.out.println(apple);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="someOne" class="com.picc.spring.depencyinjection.SomeOne" p:age="12" p:name="xxx">
</bean>
<bean id="someBean" class="com.picc.spring.depencyinjection.SomeBean">
<property name="sex" value="man"></property>
<property name="list">
<list>
<ref bean="someOne"></ref>
<value>111</value>
</list>
</property>
<property name="set">
<set>
<ref bean="someOne"></ref>
<value>444</value>
</set>
</property>
<property name="map">
<map>
<entry key="aaa" value-ref="someOne"></entry>
<entry key="bbb" value="666"></entry>
<entry key-ref="someOne" value-ref="someOne"></entry>
</map>
</property>
<property name="prop">
<props>
<prop key="a">123</prop>
<prop key="b">456</prop>
</props>
</property>
</bean>
</beans>