[Spring5]IOC容器_Bean管理XML方式_注入集合类型属性
生活随笔
收集整理的這篇文章主要介紹了
[Spring5]IOC容器_Bean管理XML方式_注入集合类型属性
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
xml注入集合屬性
1.注入數(shù)組類(lèi)型屬性
2.注入List集合類(lèi)型屬性
3.注入Map集合類(lèi)型屬性
(1)創(chuàng)建類(lèi),定義數(shù)組,list,map,set類(lèi)型屬性,生成對(duì)應(yīng)set方法
package com.atguigu.collectiontype;import java.util.List; import java.util.Map; import java.util.Set;public class Stu {//1.數(shù)組類(lèi)型屬性private String[] courses;//2.List集合類(lèi)型屬性private List<String> list;//3.map集合類(lèi)型屬性private Map<String,String> maps;//4.set集合類(lèi)型屬性private Set<String> sets;public void setSets(Set<String> sets) {this.sets = sets;}public void setList(List<String> list) {this.list = list;}public void setMaps(Map<String, 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配置文件中進(jìn)行配置
<?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.xsd"><!--1.集合類(lèi)型屬性注入--><bean id = "stu" class = "com.atguigu.collectiontype.Stu"><!--數(shù)組類(lèi)型屬性注入--><property name="courses"><array><value>java課程</value><value>數(shù)據(jù)庫(kù)課程</value></array></property><!--list類(lèi)型屬性注入--><property name="list"><list><value>張三</value><value>小三</value></list></property><!--map類(lèi)型屬性注入--><property name="maps"><map><entry key = "JAVA" value="java"></entry><entry key = "PHP" value="php"></entry></map></property><!--set類(lèi)型屬性注入--><property name="sets"><set><value>MySQL</value><value>Redis</value></set></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();} }在集合里面設(shè)置對(duì)象類(lèi)型值
package com.atguigu.collectiontype;public class Course {private String cname;//課程名稱(chēng)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.數(shù)組類(lèi)型屬性private String[] courses;//2.List集合類(lèi)型屬性private List<String> list;//3.map集合類(lèi)型屬性private Map<String,String> maps;//4.set集合類(lèi)型屬性private Set<String> sets;//學(xué)生所學(xué)多門(mén)課程private List<Course> courseList;public void setCourseList(List<Course> courseList) {this.courseList = courseList;}public void setSets(Set<String> sets) {this.sets = sets;}public void setList(List<String> list) {this.list = list;}public void setMaps(Map<String, 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 version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://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.集合類(lèi)型屬性注入--><bean id = "stu" class = "com.atguigu.collectiontype.Stu"><!--數(shù)組類(lèi)型屬性注入--><property name="courses"><array><value>java課程</value><value>數(shù)據(jù)庫(kù)課程</value></array></property><!--list類(lèi)型屬性注入--><property name="list"><list><value>張三</value><value>小三</value></list></property><!--map類(lèi)型屬性注入--><property name="maps"><map><entry key = "JAVA" value="java"></entry><entry key = "PHP" value="php"></entry></map></property><!--set類(lèi)型屬性注入--><property name="sets"><set><value>MySQL</value><value>Redis</value></set></property><!--注入list集合類(lèi)型,值是對(duì)象--><property name="courseList"><list><ref bean="course1"></ref><ref bean = "course2"></ref></list></property></bean><!--創(chuàng)建多個(gè)course對(duì)象--><bean id = "course1" class = "com.atguigu.collectiontype.Course"><property name="cname" value="Spring5框架"></property></bean><bean id = "course2" class = "com.atguigu.collectiontype.Course"><property name="cname" value="MySql5框架"></property></bean></beans>測(cè)試:
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();} }把集合注入部分提取出來(lái)
(1)在spring配置文件中引入名稱(chēng)空間util
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://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"></beans>(2)使用util標(biāo)簽完成list集合注入提取
package com.atguigu.spring.collectiontype;import java.util.List;public class Book {private List<String> list;public void setList(List<String> list){this.list = list;}public void test(){System.out.println(list);} } <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://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集合類(lèi)型屬性注入--><util:list id = "bookList"><value>易筋經(jīng)</value><value>九陰真經(jīng)</value><value>九陽(yáng)神功</value></util:list><!--2.提取list集合類(lèi)型屬性注入使用--><bean id = "book" class = "com.atguigu.spring.collectiontype.Book"><property name="list" ref = "bookList"></property></bean></beans>測(cè)試:
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();}} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的[Spring5]IOC容器_Bean管理XML方式_注入集合类型属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 响应式设计
- 下一篇: 监控工程里所使的交换机选型有哪些误区监控