网站推广适合哪种公司做,软文营销的五大注意事项,广州网站设计哪家公司好,长椿街网站建设xml注入集合属性
1.注入数组类型属性
2.注入List集合类型属性
3.注入Map集合类型属性
#xff08;1#xff09;创建类#xff0c;定义数组#xff0c;list#xff0c;map#xff0c;set类型属性#xff0c;生成对应set方法
package com.atguigu.collectiontype;imp…xml注入集合属性
1.注入数组类型属性
2.注入List集合类型属性
3.注入Map集合类型属性
1创建类定义数组listmapset类型属性生成对应set方法
package com.atguigu.collectiontype;import java.util.List;
import java.util.Map;
import java.util.Set;public class Stu {//1.数组类型属性private String[] courses;//2.List集合类型属性private ListString list;//3.map集合类型属性private MapString,String maps;//4.set集合类型属性private SetString sets;public void setSets(SetString sets) {this.sets sets;}public void setList(ListString list) {this.list list;}public void setMaps(MapString, String maps) {this.maps maps;}public void setCourses(String[] courses){this.courses courses;}public void test(){System.out.println(Arrays.toString(courses));System.out.println(list);System.out.println(maps);System.out.println(sets);}}
2在spring配置文件中进行配置
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--1.集合类型属性注入--bean id stu class com.atguigu.collectiontype.Stu!--数组类型属性注入--property namecoursesarrayvaluejava课程/valuevalue数据库课程/value/array/property!--list类型属性注入--property namelistlistvalue张三/valuevalue小三/value/list/property!--map类型属性注入--property namemapsmapentry key JAVA valuejava/entryentry key PHP valuephp/entry/map/property!--set类型属性注入--property namesetssetvalueMySQL/valuevalueRedis/value/set/property/bean/beanspackage com.atguigu.test;import com.atguigu.collectiontype.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testCollection {Testpublic void testCollection(){ApplicationContext context new ClassPathXmlApplicationContext(bean5.xml);Stu stu context.getBean(stu, Stu.class);stu.test();}
}在集合里面设置对象类型值
package com.atguigu.collectiontype;public class Course {private String cname;//课程名称public void setCname(String cname) {this.cname cname;}Overridepublic String toString() {return Course{ cname cname \ };}
}
package com.atguigu.collectiontype;import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;public class Stu {//1.数组类型属性private String[] courses;//2.List集合类型属性private ListString list;//3.map集合类型属性private MapString,String maps;//4.set集合类型属性private SetString sets;//学生所学多门课程private ListCourse courseList;public void setCourseList(ListCourse courseList) {this.courseList courseList;}public void setSets(SetString sets) {this.sets sets;}public void setList(ListString list) {this.list list;}public void setMaps(MapString, String maps) {this.maps maps;}public void setCourses(String[] courses){this.courses courses;}public void test(){System.out.println(Arrays.toString(courses));System.out.println(list);System.out.println(maps);System.out.println(sets);System.out.println(courseList);}}
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd!--1.集合类型属性注入--bean id stu class com.atguigu.collectiontype.Stu!--数组类型属性注入--property namecoursesarrayvaluejava课程/valuevalue数据库课程/value/array/property!--list类型属性注入--property namelistlistvalue张三/valuevalue小三/value/list/property!--map类型属性注入--property namemapsmapentry key JAVA valuejava/entryentry key PHP valuephp/entry/map/property!--set类型属性注入--property namesetssetvalueMySQL/valuevalueRedis/value/set/property!--注入list集合类型值是对象--property namecourseListlistref beancourse1/refref bean course2/ref/list/property/bean!--创建多个course对象--bean id course1 class com.atguigu.collectiontype.Courseproperty namecname valueSpring5框架/property/beanbean id course2 class com.atguigu.collectiontype.Courseproperty namecname valueMySql5框架/property/bean/beans测试
package com.atguigu.test;
import com.atguigu.collectiontype.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testCollection {Testpublic void testCollection(){ApplicationContext context new ClassPathXmlApplicationContext(bean5.xml);Stu stu context.getBean(stu, Stu.class);stu.test();}
}把集合注入部分提取出来
1在spring配置文件中引入名称空间util
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd/beans2使用util标签完成list集合注入提取
package com.atguigu.spring.collectiontype;import java.util.List;public class Book {private ListString list;public void setList(ListString list){this.list list;}public void test(){System.out.println(list);}
}
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd!--1.提取list集合类型属性注入--util:list id bookListvalue易筋经/valuevalue九阴真经/valuevalue九阳神功/value/util:list!--2.提取list集合类型属性注入使用--bean id book class com.atguigu.spring.collectiontype.Bookproperty namelist ref bookList/property/bean/beans测试
package com.atguigu.spring.test;import com.atguigu.spring.collectiontype.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testBook {Testpublic void testCollection(){ApplicationContext context new ClassPathXmlApplicationContext(bean1.xml);Book book context.getBean(book, Book.class);book.test();}}