在Spring中相应的可以用来定义和设置类型为java的List,Set,Map和Properties。 对集合的注入方法进行总结: 对于List,Set,Map和Properties 来说都是类似的,都是要先把注入的信息注入到集合中去,然后再把集合注入到相关的Bean中。 ======================练习一:List================= ------------------------------------HelloWord 类------------------------------- package com.gc.action; import java.util.List; public class HelloWord { private List msg; public HelloWord() { super(); } public HelloWord(List msg) { super(); this.msg = msg; } //getXxx和SetXxx public List getMsg() { return msg; } public void setMsg(List msg) { this.msg = msg; } } ------------------------config.xml----------------------------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="HelloWord" class="com.gc.action.HelloWord" > <property name="msg"> <list> <value>zx1</value> <value>zx2</value> <value>zx3</value> </list> </property> </bean> </beans> ----------------------------TestHelloWord 测试类---------------- package com.gc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.gc.action.HelloWord; public class TestHelloWord { /** * @param args */ public static void main(String[] args) { ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/config.xml"); HelloWord hw =(HelloWord) ac.getBean("HelloWord"); System.out.println(hw.getMsg()); } } ---------------结果--------------- [zx1, zx2, zx3] ====================练习二:Set================= -------------------HelloWord 类---------------- package com.gc.action; import java.util.List; import java.util.Set; public class HelloWord { private Set msg; public HelloWord() { super(); } public HelloWord(Set msg) { super(); this.msg = msg; } //getXxx和SetXxx public Set getMsg() { return msg; } public void setMsg(Set msg) { this.msg = msg; } } ---------------------------------config.xml----------------------------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="HelloWord" class="com.gc.action.HelloWord" > <property name="msg"> <set> <value>111</value> <value>222</value> <value>333</value> </set> </property> </bean> </beans> --------------------TestHelloWord 测试类---------------------------- package com.gc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.gc.action.HelloWord; public class TestHelloWord { public static void main(String[] args) { ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/config.xml"); HelloWord hw =(HelloWord) ac.getBean("HelloWord"); System.out.println(hw.getMsg()); } } -------------------结果------------- [111, 222, 333] ======================练习三:Map================== package com.gc.action; import java.util.Map; import java.util.Set; public class HelloWord { private Map msg; public HelloWord() { super(); } public HelloWord(Map msg) { super(); this.msg = msg; } public Map getMsg() { return msg; } public void setMsg(Map msg) { this.msg = msg; } } -----------------------config.xml------------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="HelloWord" class="com.gc.action.HelloWord" > <property name="msg"> <map> <entry key="z"> <value>aaa </value> </entry> <entry key="x"> <value>bbb </value> </entry> </map> </property> </bean> </beans> --------------------------TestHelloWord 测试类------------ package com.gc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.gc.action.HelloWord; public class TestHelloWord { public static void main(String[] args) { ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/config.xml"); HelloWord hw =(HelloWord) ac.getBean("HelloWord"); System.out.println(hw.getMsg()); } } ----------------结果------------- {z=aaa , x=bbb } ==========================练习四:Properties=============== --------------HelloWord 类--------------------- package com.gc.action; import java.util.Properties; public class HelloWord { private Properties msg; public HelloWord(Properties msg) { super(); this.msg = msg; } public HelloWord() { super(); } public Properties getMsg() { return msg; } public void setMsg(Properties msg) { this.msg = msg; } } -----------------------------config.xml-------------------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="HelloWord" class="com.gc.action.HelloWord" > <property name="msg"> <props> <prop key="zz">aaa</prop> <prop key="xx">bbb</prop> </props> </property> </bean> </beans> --------------------------------TestHelloWord测试类----------------------------- package com.gc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.gc.action.HelloWord; public class TestHelloWord { public static void main(String[] args) { ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/config.xml"); HelloWord hw =(HelloWord) ac.getBean("HelloWord"); System.out.println(hw.getMsg()); } } -------------------结果------------ {zz=aaa, xx=bbb}
|
|
来自: 樱花梦_张艺馨 > 《Spring从入门到精通》