Spring注入List和Map

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Construct List -->
<bean id="nameList" class="java.util.ArrayList">
   <constructor-arg>
    <list>
     <value>liuyu</value>
    </list>
   </constructor-arg>
</bean>
<!-- Construct Map -->
<bean id="nameMap" class="java.util.HashMap">
   <constructor-arg>
    <map>
     <entry key="liuyu" value-ref="nameList" />
    </map>
   </constructor-arg>
</bean>
<!-- Inject List and Map -->
<bean id="myDemo" class="spring.inject.demo.MyDemo">
   <property name="nameList">
    <ref local="nameList" />
   </property>
   <property name="nameMap">
    <ref local="nameMap" />
   </property>
</bean>
</beans>



public class MyDemo {
private List<String> nameList = null;
private Map<String, List<String>> nameMap = null;
public List<String> getNameList() {
   return nameList;
}
public void setNameList(List<String> nameList) {
   this.nameList = nameList;
}
public Map<String, List<String>> getNameMap() {
   return nameMap;
}
public void setNameMap(Map<String, List<String>> nameMap) {
   this.nameMap = nameMap;
}
}

你可能感兴趣的:(Spring注入)