日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

IOC操作Bean管理XML方式(注入集合类型属性)

發(fā)布時(shí)間:2024/7/23 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOC操作Bean管理XML方式(注入集合类型属性) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

IOC操作Bean管理XML方式(注入集合類型屬性)

(1)首先進(jìn)行環(huán)境的搭建和準(zhǔn)備

(2)創(chuàng)建一個(gè)類:用來完成集合類型屬性注入

(3)在Spring 配置文件進(jìn)行配置

(4)編寫一個(gè)測試類進(jìn)行測試

(5)運(yùn)行結(jié)果:

(6)注意細(xì)節(jié):

(7)據(jù)上面所述,在集合里面設(shè)置對象類型值

(8)把集合注入部分提取出來,變成通用部分

(8.1)在Spring 配置文件中引入名稱空間 util

(8.2)使用 util 標(biāo)簽完成 list 集合注入提取


?

IOC操作Bean管理XML方式(注入集合類型屬性)

1.注入數(shù)組類型屬性

2.注入 List 集合類型屬性

3.注入 Map 集合類型屬性

?

?

?

(1)首先進(jìn)行環(huán)境的搭建和準(zhǔn)備

新建一個(gè)collectiontype包

?

(2)創(chuàng)建一個(gè)類:用來完成集合類型屬性注入

進(jìn)行實(shí)體類編寫

?

寫出三個(gè)基本類型

包括:數(shù)組類型、List集合、Map集合、Set集合(同時(shí)生成對應(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ù)組類型屬性private String[] courses;//2.list 集合類型屬性private List<String> list;//3.map 集合類型屬性private Map<String,String> maps;//4.set 集合類型屬性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;} // 寫一個(gè)測試方法方便輸出查看public void test(){ // 數(shù)組如果直接輸出的話不是一個(gè) 數(shù)值 // System.out.println(courses);// 因此我們引入工具類來方便輸出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=""只能寫入一個(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"><!--集合類型屬性注入--><bean id="student" class="com.lbj.spring5.collectiontype.Student"><!--1.數(shù)組類型屬性注入--><property name="courses" ><array><value>語文</value><value>英語</value><value>數(shù)學(xué)</value></array></property><!--2.List類型屬性注入--><property name="list"><list><value>張三</value><value>王五</value></list></property><!--3.Map類型屬性注入--><property name="maps"><map><entry key="JAVA" value="java"></entry><entry key="PHP" value="php"></entry></map></property><!--4.Set類型屬性注入--><property name="sets"><set><value>123</value><value>321</value></set></property></bean> </beans>

?

(4)編寫一個(gè)測試類進(jìn)行測試

創(chuàng)建一個(gè)新的包 testdemo包

在 testdemo包 新建一個(gè)?TestSpring5Demo1類

代碼如下:

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:在上面例子中,集合的類型都是String 字符串類型

思考,以后在String類型中,寫入的是對象,那該如何處理呢?

?

細(xì)節(jié)2:例子所示的4個(gè)集合都只能在Student類內(nèi)進(jìn)行調(diào)用使用,但是以后需要這些集合可以被其它類調(diào)用,該如何處理

思考:集合是不是可以進(jìn)行抽取,做成公共部分?

?

(7)據(jù)上面所述,在集合里面設(shè)置對象類型值

步驟:新建一個(gè)課程類 Course

Course類代碼如下:

package com.lbj.spring5.collectiontype;/*** 課程類*/ public class Course { // 課程名稱private String cname;public void setCname(String cname) {this.cname = cname;}// 為了方便輸出才寫這個(gè)方法,正常情況下輸出的值為[com.lbj.spring5.collectiontype.Course@306279ee, com.lbj.spring5.collectiontype.Course@545997b1] // 寫了這個(gè)toString 方法后我們就可以進(jìn)行正常值的輸出@Overridepublic String toString() {return "Course{" +"cname='" + cname + '\'' +'}';} }

?

下一步,到Student類中,補(bǔ)充如下代碼:

// 學(xué)生所學(xué)多門課程private List<Course> courseList;public void setCourseList(List<Course> courseList) {this.courseList = courseList;}

?

同樣,為了測試,在Student類中的 test() 方法中加入如下輸出語句

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集合類型,值是對象的時(shí)候--><property name="courseList"><list><!--引入對象的id值--><ref bean="course1"></ref><ref bean="course2"></ref></list></property></bean><!--操作:創(chuàng)建多個(gè)course對象--><!--寫入第一個(gè)課程--><bean id="course1" class="com.lbj.spring5.collectiontype.Course"><property name="cname" value="英語"></property></bean><!--寫入第二個(gè)課程--><bean id="course2" class="com.lbj.spring5.collectiontype.Course"><property name="cname" value="數(shù)學(xué)"></property></bean> </beans>

?

進(jìn)行測試:

?

(8)把集合注入部分提取出來,變成通用部分

步驟:新建一個(gè)bean2.xml

?

步驟:新建一個(gè)類,專門做提取部分

代碼如下:

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;}//測試方法public void test(){System.out.println(list);} }

(8.1)在Spring 配置文件中引入名稱空間 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: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 集合類型屬性注入--><util:list id="booklist"><value>英語書</value><value>數(shù)學(xué)書</value><value>語文書</value></util:list><!--2.然后將提取 list 集合類型屬性注入--><bean id="book" class="com.lbj.spring5.collectiontype.Book"><property name="list" ref="booklist"></property></bean> </beans>

進(jìn)行測試:

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();} }

測試結(jié)果:

?

?

?

總結(jié)

以上是生活随笔為你收集整理的IOC操作Bean管理XML方式(注入集合类型属性)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。