首页 > 代码库 > spring 注入集合类型 List、Set、Map
spring 注入集合类型 List、Set、Map
首先先看用到的类:
Class1,Class2
代码:
package my.spring.fuck;public class Class1 implements Interface1{ private String a; public String getA() { return a; } public void setA(String a) { this.a = a; } public void eat(String s){ System.out.println(s+a); }}
package my.spring.fuck;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Class2 { private List<Class1> fuckList1;//集合统一装入Class1 private List fuckList2; private Set<Class1> fuckSet1; private Set fuckSet2; private Map<String,String> fuckMap1; private Map<String,Class1> fuckMap2; public static void main(String[] args){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); //Class2 c2=(Class2)ac.getBean("c2"); Class2 c2=ac.getBean(Class2.class); c2.print(); } public void print(){ System.out.println("this is fuckList1"); for(Class1 c1:fuckList1){ c1.eat(""); } System.out.println("this is fuckList2"); for(int i=0;i<fuckList2.size();i++){ ((Class1)fuckList2.get(i)).eat(""); } System.out.println("this is fuckSet1"); for(Class1 c1:fuckSet1){ c1.eat(""); } System.out.println("this is fuckSet2"); Iterator<Class1> iterator=fuckSet2.iterator(); while(iterator.hasNext()){ (iterator.next()).eat(""); } System.out.println("this is fuckMap1"); System.out.println(fuckMap1.get("mapKey1")); System.out.println("this is fuckMap2"); Class1 c1=fuckMap2.get("mapKey1"); c1.eat(""); } public List<Class1> getFuckList1() { return fuckList1; } public void setFuckList1(List<Class1> fuckList1) { this.fuckList1 = fuckList1; } public List getFuckList2() { return fuckList2; } public void setFuckList2(List fuckList2) { this.fuckList2 = fuckList2; } public Set<Class1> getFuckSet1() { return fuckSet1; } public void setFuckSet1(Set<Class1> fuckSet1) { this.fuckSet1 = fuckSet1; } public Set getFuckSet2() { return fuckSet2; } public void setFuckSet2(Set fuckSet2) { this.fuckSet2 = fuckSet2; } public Map<String, String> getFuckMap1() { return fuckMap1; } public void setFuckMap1(Map<String, String> fuckMap1) { this.fuckMap1 = fuckMap1; } public Map<String, Class1> getFuckMap2() { return fuckMap2; } public void setFuckMap2(Map<String, Class1> fuckMap2) { this.fuckMap2 = fuckMap2; } }
application.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="c2" class="my.spring.fuck.Class2"> <property name="fuckList1"> <list> <ref bean="c1"/> <ref bean="c1"/> </list> </property> <property name="fuckList2"> <list> <ref bean="c1"/> <ref bean="c1"/> </list> </property> <property name="fuckSet1"> <set> <ref bean="c1"/> <ref bean="c1"/> </set> </property> <property name="fuckSet2"> <set> <ref bean="c1"/> <ref bean="c1"/> </set> </property> <property name="fuckMap1"> <map> <entry key="mapKey1" value="mapValue1"/> </map> </property> <property name="fuckMap2"> <map> <entry key="mapKey1"> <ref bean="c1"/> </entry> </map> </property> </bean> <bean id="c1" class="my.spring.fuck.Class1"> <property name="a" value="i am a"> </property> </bean></beans>
运行Class2,控制台输出如下:
this is fuckList1i am ai am athis is fuckList2i am ai am athis is fuckSet1i am athis is fuckSet2i am athis is fuckMap1mapValue1this is fuckMap2i am a
spring 注入集合类型 List、Set、Map
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。