欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring?IOC容器的Bean管理XML自動(dòng)裝配過程

 更新時(shí)間:2022年05月30日 15:47:41   作者:把蘋果咬哭的測試筆記  
這篇文章主要為大家介紹了spring?IOC容器Bean管理基于XML的自動(dòng)裝配過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

什么是自動(dòng)裝配?

在之前的內(nèi)容中,每給屬性注入值都要一個(gè)個(gè)的用 property 標(biāo)簽來完成,比如:

<bean id="book" class="com.pingguo.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>

這就是手動(dòng)裝配。

而自動(dòng)裝配中,spring 會(huì)根據(jù)指定裝配規(guī)則(屬性名稱或者屬性類型) 來自動(dòng)的將匹配的屬性值進(jìn)行注入。

自動(dòng)裝配過程

1. 創(chuàng)建 2 個(gè)類

分別是部門類 Department 和員工類 Employee 。

package com.pingguo.spring5.autowire;
public class Department {
    @Override
    public String toString() {
        return "Department{}";
    }
}

員工類有個(gè) 部門的屬性,表示員工所屬的一個(gè)部門。其他方法是為了后續(xù)方便演示輸出。

package com.pingguo.spring5.autowire;
public class Employee {
    private Department department;
    public void setDepartment(Department department) {
        this.department = department;
    }
    @Override
    public String toString() {
        return "Employee{" +
                "department=" + department +
                '}';
    }
    public void test() {
        System.out.println(department);
    }
}

2. 配置文件

<?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="employee" class="com.pingguo.spring5.autowire.Employee">
        <property name="department" ref="department"></property>
    </bean>
    <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>
</beans>

3. 測試方法

@Test
    public void test5() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean5.xml");
        Employee employee = context.getBean("employee", Employee.class);
        System.out.println(employee);
    }

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

Employee{department=Department{}}
Process finished with exit code 0

ok,到這里,其實(shí)就是手動(dòng)裝配的過程。

實(shí)現(xiàn)自動(dòng)裝配,在配置文件里,通過 bean 標(biāo)簽里的屬性 autowire 來配置:

  • autowire="byName":根據(jù)屬性名稱自動(dòng)注入。
  • autowire="byType":根據(jù)屬性類型自動(dòng)注入。

1)byName 演示

注入值的bean的 id 值和類屬性名稱一致,比如:

修改配置文件,加上 autowire="byName",然后注釋掉 property。

<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byName">
        <!--<property name="department" ref="department"></property>-->
    </bean>
    <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>

執(zhí)行測試函數(shù):

Employee{department=Department{}}
Process finished with exit code 0

跟使用 property 手動(dòng)裝配結(jié)果一致。

2)byType 演示

要注入值的 bean 的類型與 屬性里的一致,比如:

現(xiàn)在繼續(xù)修改配置文件,加上 autowire="byType",然后注釋掉 property。

<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byType">
        <!--<property name="department" ref="department"></property>-->
    </bean>
    <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>

再次執(zhí)行測試:

Employee{department=Department{}}
Process finished with exit code 0

跟使用 property 手動(dòng)裝配結(jié)果一致。

不過,用 xml 方式使用自動(dòng)裝配實(shí)際中是很少的,一般是以注解的方式,后續(xù)會(huì)學(xué)習(xí)到。

以上就是spring IOC容器的Bean管理XML自動(dòng)裝配過程的詳細(xì)內(nèi)容,更多關(guān)于spring IOC Bean管理XML裝配的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringController返回值和異常自動(dòng)包裝的問題小結(jié)

    SpringController返回值和異常自動(dòng)包裝的問題小結(jié)

    今天遇到一個(gè)需求,在不改動(dòng)原系統(tǒng)代碼的情況下,將Controller的返回值和異常包裝到一個(gè)統(tǒng)一的返回對(duì)象中去,下面通過本文給大家介紹SpringController返回值和異常自動(dòng)包裝的問題,需要的朋友可以參考下
    2024-03-03
  • java編程常用技術(shù)(推薦)

    java編程常用技術(shù)(推薦)

    下面小編就為大家?guī)硪黄猨ava編程常用技術(shù)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • Mybatis返回插入主鍵id的方法

    Mybatis返回插入主鍵id的方法

    這篇文章主要介紹了 Mybatis返回插入主鍵id的方法,在文章底部給大家補(bǔ)充了Mybatis中insert中返回主鍵ID的方法,非常不錯(cuò),需要的朋友可以參考下
    2017-04-04
  • 淺談spring和spring MVC的區(qū)別與關(guān)系

    淺談spring和spring MVC的區(qū)別與關(guān)系

    下面小編就為大家?guī)硪黄獪\談spring和spring MVC的區(qū)別與關(guān)系。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • springboot打成jar后獲取classpath下文件失敗的解決方案

    springboot打成jar后獲取classpath下文件失敗的解決方案

    這篇文章主要介紹了使用springboot打成jar后獲取classpath下文件失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring security如何重寫Filter實(shí)現(xiàn)json登錄

    Spring security如何重寫Filter實(shí)現(xiàn)json登錄

    這篇文章主要介紹了Spring security 如何重寫Filter實(shí)現(xiàn)json登錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解Spring ApplicationContext加載過程

    詳解Spring ApplicationContext加載過程

    這篇文章主要介紹了Spring ApplicationContext加載過程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下
    2021-03-03
  • map實(shí)現(xiàn)按value升序排序

    map實(shí)現(xiàn)按value升序排序

    map內(nèi)部是按照hash算法存儲(chǔ)的,但如果能對(duì)map排序在某些時(shí)候還是有用的,下面實(shí)現(xiàn)對(duì)map按照value升序排序,實(shí)現(xiàn)對(duì)map按照key排序,大家參考使用吧
    2014-01-01
  • SpringBoot中接收POST參數(shù)的幾種方式詳解

    SpringBoot中接收POST參數(shù)的幾種方式詳解

    這篇文章主要介紹了SpringBoot中接收POST參數(shù)的幾種方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • java實(shí)現(xiàn)兩個(gè)文件的拼接

    java實(shí)現(xiàn)兩個(gè)文件的拼接

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)兩個(gè)文件的拼接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評(píng)論