IOC操作Bean管理XML方式(注入集合类型属性)
目錄
IOC操作Bean管理XML方式(注入集合類(lèi)型屬性)
(1)首先進(jìn)行環(huán)境的搭建和準(zhǔn)備
(2)創(chuàng)建一個(gè)類(lèi):用來(lái)完成集合類(lèi)型屬性注入
(3)在Spring 配置文件進(jìn)行配置
(4)編寫(xiě)一個(gè)測(cè)試類(lèi)進(jìn)行測(cè)試
(5)運(yùn)行結(jié)果:
(6)注意細(xì)節(jié):
(7)據(jù)上面所述,在集合里面設(shè)置對(duì)象類(lèi)型值
(8)把集合注入部分提取出來(lái),變成通用部分
(8.1)在Spring 配置文件中引入名稱(chēng)空間 util
(8.2)使用 util 標(biāo)簽完成 list 集合注入提取
?
IOC操作Bean管理XML方式(注入集合類(lèi)型屬性)
1.注入數(shù)組類(lèi)型屬性
2.注入 List 集合類(lèi)型屬性
3.注入 Map 集合類(lèi)型屬性
?
?
?
(1)首先進(jìn)行環(huán)境的搭建和準(zhǔn)備
新建一個(gè)collectiontype包
?
(2)創(chuàng)建一個(gè)類(lèi):用來(lái)完成集合類(lèi)型屬性注入
進(jìn)行實(shí)體類(lèi)編寫(xiě)
?
寫(xiě)出三個(gè)基本類(lèi)型
包括:數(shù)組類(lèi)型、List集合、Map集合、Set集合(同時(shí)生成對(duì)應(yīng)的set方法)
package com.lbj.spring5.collectiontype;import java.util.List; import java.util.Map; import java.util.Set;public class Student {//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 setCourses(String[] courses) {this.courses = courses;}public void setList(List<String> list) {this.list = list;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public void setSets(Set<String> sets) {this.sets = sets;} // 寫(xiě)一個(gè)測(cè)試方法方便輸出查看public void test(){ // 數(shù)組如果直接輸出的話(huà)不是一個(gè) 數(shù)值 // System.out.println(courses);// 因此我們引入工具類(lèi)來(lái)方便輸出System.out.println(Arrays.toString(courses));System.out.println(list);System.out.println(maps);System.out.println(sets);} }?
(3)在Spring 配置文件進(jìn)行配置
注意:<property name="courses" value="">其中的value=""只能寫(xiě)入一個(gè)值,并不符合數(shù)組多個(gè)值的要求
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--集合類(lèi)型屬性注入--><bean id="student" class="com.lbj.spring5.collectiontype.Student"><!--1.數(shù)組類(lèi)型屬性注入--><property name="courses" ><array><value>語(yǔ)文</value><value>英語(yǔ)</value><value>數(shù)學(xué)</value></array></property><!--2.List類(lèi)型屬性注入--><property name="list"><list><value>張三</value><value>王五</value></list></property><!--3.Map類(lèi)型屬性注入--><property name="maps"><map><entry key="JAVA" value="java"></entry><entry key="PHP" value="php"></entry></map></property><!--4.Set類(lèi)型屬性注入--><property name="sets"><set><value>123</value><value>321</value></set></property></bean> </beans>?
(4)編寫(xiě)一個(gè)測(cè)試類(lèi)進(jìn)行測(cè)試
創(chuàng)建一個(gè)新的包 testdemo包
在 testdemo包 新建一個(gè)?TestSpring5Demo1類(lèi)
代碼如下:
package com.lbj.spring5.testdemo;import com.lbj.spring5.collectiontype.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void tsetCollection(){ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");Student student=context.getBean("student", Student.class);student.test();} }(5)運(yùn)行結(jié)果:
?
(6)注意細(xì)節(jié):
細(xì)節(jié)1:在上面例子中,集合的類(lèi)型都是String 字符串類(lèi)型
思考,以后在String類(lèi)型中,寫(xiě)入的是對(duì)象,那該如何處理呢?
?
細(xì)節(jié)2:例子所示的4個(gè)集合都只能在Student類(lèi)內(nèi)進(jìn)行調(diào)用使用,但是以后需要這些集合可以被其它類(lèi)調(diào)用,該如何處理
思考:集合是不是可以進(jìn)行抽取,做成公共部分?
?
(7)據(jù)上面所述,在集合里面設(shè)置對(duì)象類(lèi)型值
步驟:新建一個(gè)課程類(lèi) Course
Course類(lèi)代碼如下:
package com.lbj.spring5.collectiontype;/*** 課程類(lèi)*/ public class Course { // 課程名稱(chēng)private String cname;public void setCname(String cname) {this.cname = cname;}// 為了方便輸出才寫(xiě)這個(gè)方法,正常情況下輸出的值為[com.lbj.spring5.collectiontype.Course@306279ee, com.lbj.spring5.collectiontype.Course@545997b1] // 寫(xiě)了這個(gè)toString 方法后我們就可以進(jìn)行正常值的輸出@Overridepublic String toString() {return "Course{" +"cname='" + cname + '\'' +'}';} }?
下一步,到Student類(lèi)中,補(bǔ)充如下代碼:
// 學(xué)生所學(xué)多門(mén)課程private List<Course> courseList;public void setCourseList(List<Course> courseList) {this.courseList = courseList;}?
同樣,為了測(cè)試,在Student類(lèi)中的 test() 方法中加入如下輸出語(yǔ)句
System.out.println(courseList);?
下一步,在xml配置文件中配置如下:
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--條件:注入的是List集合類(lèi)型,值是對(duì)象的時(shí)候--><property name="courseList"><list><!--引入對(duì)象的id值--><ref bean="course1"></ref><ref bean="course2"></ref></list></property></bean><!--操作:創(chuàng)建多個(gè)course對(duì)象--><!--寫(xiě)入第一個(gè)課程--><bean id="course1" class="com.lbj.spring5.collectiontype.Course"><property name="cname" value="英語(yǔ)"></property></bean><!--寫(xiě)入第二個(gè)課程--><bean id="course2" class="com.lbj.spring5.collectiontype.Course"><property name="cname" value="數(shù)學(xué)"></property></bean> </beans>?
進(jìn)行測(cè)試:
?
(8)把集合注入部分提取出來(lái),變成通用部分
步驟:新建一個(gè)bean2.xml
?
步驟:新建一個(gè)類(lèi),專(zhuān)門(mén)做提取部分
代碼如下:
package com.lbj.spring5.collectiontype;import java.util.List;public class Book {private List<String> list;public void setList(List<String> list) {this.list = list;}//測(cè)試方法public void test(){System.out.println(list);} }(8.1)在Spring 配置文件中引入名稱(chēng)空間 util
固定寫(xiě)法:
<?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:p="http://www.springframework.org/schema/p"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>(8.2)使用 util 標(biāo)簽完成 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:p="http://www.springframework.org/schema/p"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>英語(yǔ)書(shū)</value><value>數(shù)學(xué)書(shū)</value><value>語(yǔ)文書(shū)</value></util:list><!--2.然后將提取 list 集合類(lèi)型屬性注入--><bean id="book" class="com.lbj.spring5.collectiontype.Book"><property name="list" ref="booklist"></property></bean> </beans>進(jìn)行測(cè)試:
package com.lbj.spring5.testdemo;import com.lbj.spring5.collectiontype.Book; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void tsetCollection2(){ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");Book book=context.getBean("book", Book.class);book.test();} }測(cè)試結(jié)果:
?
?
?
總結(jié)
以上是生活随笔為你收集整理的IOC操作Bean管理XML方式(注入集合类型属性)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JAVA进阶教学之(StringBuid
- 下一篇: python怎么索引json中的值_使用