詳解Spring注入集合(數(shù)組、List、Map、Set)類型屬性
注入集合(數(shù)組、List、Map、Set)類型屬性
(1)創(chuàng)建類,定義數(shù)組,list,map,set類型屬性,并且生成對(duì)應(yīng)的set方法。
(2)在spring配置文件中進(jìn)行配置。
Stu類:
package com.Keafmd.spring5.collectiontype; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; /** * Keafmd * * @ClassName: Stu * @Description: IOC操作Bean管理(xml注入屬性集合) * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:15 */ public class Stu { //1、數(shù)組類型屬性 private String[] courses; //2、list集合類型屬性 private List<String> list; //3、map集合類型屬性 private Map<String,String> maps; //4、set集合類型屬性 private Set<String> sets; //學(xué)生所學(xué)的多門課程 private List<Course> courseList; public void setCourseList(List<Course> courseList) { this.courseList = courseList; } 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; } 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); } }
bean1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--集合類型屬性注入--> <bean id="stu" class="com.Keafmd.spring5.collectiontype.Stu"> <!--數(shù)組類型屬性注入--> <property name="courses"> <array> <value>Java</value> <value>C++</value> <value>Python</value> </array> </property> <!--list類型屬性注入--> <property name="list"> <list> <value>小明</value> <value>小紅</value> </list> </property> <!--map類型屬性注入--> <property name="maps"> <map> <entry key="Java" value="java"></entry> <entry key="C++" value="c++"></entry> </map> </property> <!--set類型屬性注入--> <property name="sets"> <set> <value>北京</value> <value>上海</value> </set> </property> <!--注入list集合類型,值是對(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.Keafmd.spring5.collectiontype.Course"> <property name="cname" value="Spring5框架"></property> </bean> <bean id="course2" class="com.Keafmd.spring5.collectiontype.Course"> <property name="cname" value="MyBatis框架"></property> </bean> </beans>
測(cè)試類:
package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 測(cè)試類 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void testCollection1(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); Stu stu = context.getBean("stu",Stu.class); stu.test(); } }
輸出結(jié)果:
[Java, C++, Python]
[小明, 小紅]
{Java=java, C++=c++}
[北京, 上海]
[Course{cname='Spring5框架'}, Course{cname='MyBatis框架'}]Process finished with exit code 0
把集合注入部分提取出來(lái)
(1)在spring配置文件中引入名稱空間util(
在配置信息中添加xmlns:util="http://www.springframework.org/schema/util"和http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd")。
(2)提取list集合類型屬性注入。
(3)把提取的list集合類型屬性注入使用。
Book類:
package com.Keafmd.spring5.collectiontype; import java.util.List; /** * Keafmd * * @ClassName: Book * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:56 */ public class Book { private List<String> list; public void setList(List<String> list) { this.list = list; } public void test(){ System.out.println(list); } }
bean2.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: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 http://www.springframework.org/schema/util/spring-util.xsd"> <!--把集合注入部分提取出來(lái)--> <!--1、提取list集合類型屬性注入--> <util:list id="bookList"> <value>老人與海</value> <value>平凡的世界</value> <value>阿甘正傳</value> </util:list> <!--2、提取list集合類型屬性注入使用--> <bean id="book" class="com.Keafmd.spring5.collectiontype.Book"> <property name="list" ref="bookList"></property> </bean> </beans>
測(cè)試代碼:
package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 測(cè)試類 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void testCollection2(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); Book book = context.getBean("book",Book.class); book.test(); } }
輸出結(jié)果:
[老人與海, 平凡的世界, 阿甘正傳]
Process finished with exit code 0
到此這篇關(guān)于詳解Spring注入集合(數(shù)組、List、Map、Set)類型屬性的文章就介紹到這了,更多相關(guān)Spring注入集合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
排查Java進(jìn)程內(nèi)存占比過(guò)高的方法
某天下午運(yùn)維反應(yīng)集成環(huán)境的一個(gè)Java服務(wù)內(nèi)存飆高,內(nèi)存耗的太高了,會(huì)疑似內(nèi)存泄漏,所以本文記一次排查Java進(jìn)程內(nèi)存占比過(guò)高的解決方法,需要的朋友可以參考下2023-10-10SpringMVC中參數(shù)綁定問(wèn)題實(shí)例詳解
springmvc是用來(lái)處理頁(yè)面的一些請(qǐng)求,然后將數(shù)據(jù)再通過(guò)視圖返回給用戶的,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中參數(shù)綁定問(wèn)題的相關(guān)資料,需要的朋友可以參考下2022-04-04解析Java編程中設(shè)計(jì)模式的開閉原則的運(yùn)用
這篇文章主要介紹了解析Java編程中設(shè)計(jì)模式的開閉原則的運(yùn)用,開閉原則多應(yīng)用于Java程序的擴(kuò)展開發(fā)方面,需要的朋友可以參考下2016-02-02Java基于分治算法實(shí)現(xiàn)的棋盤覆蓋問(wèn)題示例
這篇文章主要介紹了Java基于分治算法實(shí)現(xiàn)的棋盤覆蓋問(wèn)題,簡(jiǎn)單描述了棋盤覆蓋問(wèn)題,并結(jié)合具體實(shí)例形式分析了java基于分治算法實(shí)現(xiàn)棋盤覆蓋問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11JAVA時(shí)間存儲(chǔ)類Period和Duration使用詳解
這篇文章主要為大家介紹了JAVA時(shí)間存儲(chǔ)類Period和Duration使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09詳解Java8如何使用Lambda表達(dá)式進(jìn)行比較
Lambda表達(dá)式,也可稱為閉包,是java8的新特性,作用是取代大部分內(nèi)部類,優(yōu)化java代碼結(jié)構(gòu),讓代碼變得更加簡(jiǎn)潔緊湊。本文將利用Lambda表達(dá)式進(jìn)行排序比較,需要的可以參考一下2022-01-01springboot掃描自定義的servlet和filter代碼詳解
本文是一篇根據(jù)作者工作經(jīng)歷總結(jié)出來(lái)的關(guān)于springboot掃描自定義的servlet和filter代碼詳解的文章,小編覺(jué)得非常不錯(cuò),這里給大家分享下,和朋友們一起學(xué)習(xí),進(jìn)步。2017-10-10Spring websocket并發(fā)發(fā)送消息異常的解決
本文主要介紹了 Spring websocket并發(fā)發(fā)送消息異常的解決,當(dāng)多個(gè)線程同時(shí)嘗試通過(guò) WebSocket 會(huì)話發(fā)送消息時(shí),會(huì)拋出異常,下面就來(lái)解決一下,感興趣的可以了解一下2023-09-09java中InputStream獲取字節(jié)大小相關(guān)方法詳解
這篇文章主要給大家介紹了關(guān)于java中InputStream獲取字節(jié)大小相關(guān)方法的相關(guān)資料,在Java中要實(shí)現(xiàn)讀取文件大小,可以使用InputStream來(lái)讀取文件的內(nèi)容,并通過(guò)獲取讀取的字節(jié)數(shù)來(lái)得到文件的大小,需要的朋友可以參考下2023-11-11