package 集合类注入;
public class Some {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return name;
}
}
package 集合类注入;
import java.util.List;
import java.util.Map;
public class SomeBean {
private String[] someStrArray;
private Some[] someObjArray;
private List someList;
private Map someMap;
public List getSomeList() {
return someList;
}
public void setSomeList(List someList) {
this.someList = someList;
}
public Map getSomeMap() {
return someMap;
}
public void setSomeMap(Map someMap) {
this.someMap = someMap;
}
public Some[] getSomeObjArray() {
return someObjArray;
}
public void setSomeObjArray(Some[] someObjArray) {
this.someObjArray = someObjArray;
}
public String[] getSomeStrArray() {
return someStrArray;
}
public void setSomeStrArray(String[] someStrArray) {
this.someStrArray = someStrArray;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="some1" class="集合类注入.Some">
<property name="name" value="tom"/>
</bean>
<bean id="some2" class="集合类注入.Some">
<property name="name" value="joy"/>
</bean>
<bean id="someBean" class="集合类注入.SomeBean">
<property name="someStrArray">
<list>
<value>Hello</value>
<value>Welcome</value>
</list>
</property>
<property name="someObjArray">
<list>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someList">
<list>
<value>hehehehe</value>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someMap">
<map>
<entry key="MapTest">
<value>Hello!TOM</value>
</entry>
<entry key="someKey1">
<ref bean="some1"/>
</entry>
</map>
</property>
</bean>
</beans>
package 集合类注入;
import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestArray {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("集合类注入//bean-config.xml");
SomeBean someBean = (SomeBean)context.getBean("someBean");
String[] strs = someBean.getSomeStrArray();
for(int i = 0; i < strs.length; i++){
System.out.println(strs[i]);
}
List someList = someBean.getSomeList();
for(Iterator it = someList.iterator(); it.hasNext();){
System.out.println(it.next());
}
}
}
public class Some {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return name;
}
}
package 集合类注入;
import java.util.List;
import java.util.Map;
public class SomeBean {
private String[] someStrArray;
private Some[] someObjArray;
private List someList;
private Map someMap;
public List getSomeList() {
return someList;
}
public void setSomeList(List someList) {
this.someList = someList;
}
public Map getSomeMap() {
return someMap;
}
public void setSomeMap(Map someMap) {
this.someMap = someMap;
}
public Some[] getSomeObjArray() {
return someObjArray;
}
public void setSomeObjArray(Some[] someObjArray) {
this.someObjArray = someObjArray;
}
public String[] getSomeStrArray() {
return someStrArray;
}
public void setSomeStrArray(String[] someStrArray) {
this.someStrArray = someStrArray;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="some1" class="集合类注入.Some">
<property name="name" value="tom"/>
</bean>
<bean id="some2" class="集合类注入.Some">
<property name="name" value="joy"/>
</bean>
<bean id="someBean" class="集合类注入.SomeBean">
<property name="someStrArray">
<list>
<value>Hello</value>
<value>Welcome</value>
</list>
</property>
<property name="someObjArray">
<list>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someList">
<list>
<value>hehehehe</value>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someMap">
<map>
<entry key="MapTest">
<value>Hello!TOM</value>
</entry>
<entry key="someKey1">
<ref bean="some1"/>
</entry>
</map>
</property>
</bean>
</beans>
package 集合类注入;
import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestArray {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("集合类注入//bean-config.xml");
SomeBean someBean = (SomeBean)context.getBean("someBean");
String[] strs = someBean.getSomeStrArray();
for(int i = 0; i < strs.length; i++){
System.out.println(strs[i]);
}
List someList = someBean.getSomeList();
for(Iterator it = someList.iterator(); it.hasNext();){
System.out.println(it.next());
}
}
}