Spring 集合注入

在 Spring 中注入 List, Set, Map土思,我們該怎么進行注入呢袋狞?,這篇文章接下來講的 就是這個凄贩。 如果你對注入不太了解的話誓军,可以看這篇文章。
Spring 依賴注入(基礎(chǔ))

List

首先我們要有個 Student

public class Student {

    private String name;

    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void study() {
        System.out.println(name + "在學(xué)習(xí)");
    }

    public String getName() {
        return name;
    }
}

接下來是管理 Student 類的 School

public class School {

    private List<Student> studentList;

    public School(List<Student> studentList) {
        this.studentList = studentList;
    }

    public void study() {
        for (Student student : studentList) {
            student.study();
        }
    }

}

接下來是 bean.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">

    <beans>
        <bean id="school" class="com.draper.School">
            <constructor-arg>
                <list>
                    <bean id="stu1" class="com.draper.Student">
                        <constructor-arg index="0">
                            <value>小俊</value>
                        </constructor-arg>
                        <constructor-arg index="1">
                            <value>14</value>
                        </constructor-arg>
                    </bean>
                    
                    <bean id="stu2" class="com.draper.Student">
                        <constructor-arg index="0">
                            <value>小貝</value>
                        </constructor-arg>
                        <constructor-arg index="1">
                            <value>14</value>
                        </constructor-arg>
                    </bean>
                </list>
            </constructor-arg>
        </bean>
    </beans>

</beans>

亦或是應(yīng)用我們上篇文章學(xué)的 在 bean 中注入 bean

<?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">

    <beans>
        <bean id="stu1" class="com.draper.Student">
            <constructor-arg index="0">
                <value>小俊</value>
            </constructor-arg>
            <constructor-arg index="1">
                <value>14</value>
            </constructor-arg>
        </bean>
        <bean id="stu2" class="com.draper.Student">
            <constructor-arg index="0">
                <value>小貝</value>
            </constructor-arg>
            <constructor-arg index="1">
                <value>14</value>
            </constructor-arg>
        </bean>
        <bean id="school" class="com.draper.School">
            <constructor-arg>
                <list>
                    <ref bean="stu1"/>
                    <ref bean="stu2"/>
                </list>
            </constructor-arg>
        </bean>
    </beans>

</beans>

對比我們前面學(xué)的總結(jié)下
<list> 的標簽其實他已經(jīng)給你封裝好了疲扎,<set>, <map>也是一樣昵时,只不過細節(jié)上有些不同, 而引入另外一個 bean 還是用了 <ref>
讓我們來測試一下

public class TestApp {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        School school = (School) context.getBean("school");
        school.study();
    }
}

Set

Set 就是把標簽換成 <set>椒丧,再修改下類就可以了

Map

為了方便演示壹甥,略微修改 Student

public class School {

    private Map<String, Student> studentMap;

    public School(Map<String, Student> studentMap) {
        this.studentMap = studentMap;
    }

    public void study(){
        Set<String> keySet = studentMap.keySet();
        for (String s : keySet) {
            studentMap.get(s).study();
        }
    }

}

修改 bean.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">

    <beans>
        <bean id="stu1" class="com.draper.Student">
            <constructor-arg index="0">
                <value>小俊</value>
            </constructor-arg>
            <constructor-arg index="1">
                <value>14</value>
            </constructor-arg>
        </bean>
        <bean id="stu2" class="com.draper.Student">
            <constructor-arg index="0">
                <value>小貝</value>
            </constructor-arg>
            <constructor-arg index="1">
                <value>14</value>
            </constructor-arg>
        </bean>
        <bean id="school" class="com.draper.School">
            <constructor-arg>
                <map>
                    <entry key="stu1">
                        <ref bean="stu1"/>
                    </entry>
                    <entry key="stu2">
                        <ref bean="stu2"/>
                    </entry>
                </map>
            </constructor-arg>
        </bean>
    </beans>

</beans>

測試代碼沒有改動


的確有點神奇哈

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市壶熏,隨后出現(xiàn)的幾起案子句柠,更是在濱河造成了極大的恐慌,老刑警劉巖棒假,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件溯职,死亡現(xiàn)場離奇詭異,居然都是意外死亡帽哑,警方通過查閱死者的電腦和手機谜酒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妻枕,“玉大人僻族,你說我怎么就攤上這事÷判常” “怎么了鹰贵?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長康嘉。 經(jīng)常有香客問我碉输,道長,這世上最難降的妖魔是什么亭珍? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任敷钾,我火速辦了婚禮枝哄,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘阻荒。我一直安慰自己挠锥,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布侨赡。 她就那樣靜靜地躺著蓖租,像睡著了一般。 火紅的嫁衣襯著肌膚如雪羊壹。 梳的紋絲不亂的頭發(fā)上蓖宦,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機與錄音油猫,去河邊找鬼稠茂。 笑死,一個胖子當(dāng)著我的面吹牛情妖,可吹牛的內(nèi)容都是我干的睬关。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼毡证,長吁一口氣:“原來是場噩夢啊……” “哼电爹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起料睛,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤藐不,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后秦效,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雏蛮,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年阱州,在試婚紗的時候發(fā)現(xiàn)自己被綠了挑秉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡苔货,死狀恐怖犀概,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情夜惭,我是刑警寧澤姻灶,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站诈茧,受9級特大地震影響产喉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一曾沈、第九天 我趴在偏房一處隱蔽的房頂上張望这嚣。 院中可真熱鬧,春花似錦塞俱、人聲如沸姐帚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽罐旗。三九已至,卻和暖如春唯蝶,著一層夾襖步出監(jiān)牢的瞬間九秀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工生棍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留颤霎,地道東北人媳谁。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓涂滴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親晴音。 傳聞我的和親對象是個殘疾皇子柔纵,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)锤躁,斷路器搁料,智...
    卡卡羅2017閱讀 134,657評論 18 139
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,159評論 2 7
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法系羞,內(nèi)部類的語法郭计,繼承相關(guān)的語法,異常的語法椒振,線程的語...
    子非魚_t_閱讀 31,631評論 18 399
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,811評論 6 342
  • 對于《中國有嘻哈》一開始我是拒絕的澎迎,一群20出頭的壞壞男女蹦跶著吵吵鬧鬧 看著也不過是娛樂圈綜藝節(jié)目的一次炒作庐杨,用...
    MT陳夢婷閱讀 450評論 0 3