最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring?IOC容器的Bean管理XML自動裝配過程

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

什么是自動裝配?

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

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

這就是手動裝配。

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

自動裝配過程

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

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

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

員工類有個 部門的屬性,表示員工所屬的一個部門。其他方法是為了后續(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);
    }

運行結果:

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

ok,到這里,其實就是手動裝配的過程。

實現(xiàn)自動裝配,在配置文件里,通過 bean 標簽里的屬性 autowire 來配置:

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

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 手動裝配結果一致。

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 手動裝配結果一致。

不過,用 xml 方式使用自動裝配實際中是很少的,一般是以注解的方式,后續(xù)會學習到。

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

相關文章

  • 遠程debug調(diào)試入門

    遠程debug調(diào)試入門

    這篇文章主要介紹了Eclipse的Debug調(diào)試技巧大全(總結),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧嗎,希望能給你帶來幫助
    2021-06-06
  • java開發(fā)AOP面向切面編程入門

    java開發(fā)AOP面向切面編程入門

    這篇文章主要介紹了java開發(fā)的AOP面向切面編程入門的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步學有所得
    2021-10-10
  • springboot整合spring-data-redis遇到的坑

    springboot整合spring-data-redis遇到的坑

    使用springboot整合redis,使用默認的序列化配置,然后使用redis-client去查詢時查詢不到相應的key.問題出在哪,怎么解決呢?下面小編給大家?guī)砹藄pringboot整合spring-data-redis遇到的坑,需要的的朋友參考下吧
    2017-04-04
  • Spring 4.0新功能:@Conditional注解詳細介紹

    Spring 4.0新功能:@Conditional注解詳細介紹

    Spring Boot的強大之處在于使用了Spring 4框架的新特性:@Conditional注釋,此注釋使得只有在特定條件滿足時才啟用一些配置。下面這篇文章主要給大家介紹了關于Spring4.0中新功能:@Conditional注解的相關資料,需要的朋友可以參考下。
    2017-09-09
  • java8 Math新增方法介紹

    java8 Math新增方法介紹

    這篇文章主要介紹了java8 Math新增方法介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • SpringBoot參數(shù)校驗之@Valid的使用詳解

    SpringBoot參數(shù)校驗之@Valid的使用詳解

    這篇文章主要通過示例為大家詳細介紹一下介紹了SpringBoot參數(shù)校驗中@Valid的使用方法,文中的示例代碼講解詳細,需要的可以參考一下
    2022-06-06
  • 基于Jenkins自動打包并部署docker環(huán)境的操作過程

    基于Jenkins自動打包并部署docker環(huán)境的操作過程

    這篇文章主要介紹了基于Jenkins自動打包并部署docker環(huán)境,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • JDK與Dubbo中的SPI詳細介紹

    JDK與Dubbo中的SPI詳細介紹

    這篇文章主要介紹了JDK中的SPI與Dubbo中的SPI,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-09-09
  • java實現(xiàn)學生成績錄入系統(tǒng)

    java實現(xiàn)學生成績錄入系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)學生成績錄入系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 基于Java回顧之反射的使用分析

    基于Java回顧之反射的使用分析

    本篇文章是對Java反射的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05

最新評論

孟津县| 沧源| 吴旗县| 丰台区| 雅安市| 怀仁县| 五河县| 玉树县| 南宁市| 西青区| 闻喜县| 大石桥市| 牟定县| 安达市| 上高县| 色达县| 从化市| 甘洛县| 岳阳市| 连州市| 大关县| 绥德县| 封开县| 伽师县| 澎湖县| 竹北市| 宜兰市| 东平县| 邢台市| 海门市| 密山市| 江城| 牟定县| 慈溪市| 惠州市| 岱山县| 正蓝旗| 剑河县| 吉安县| 邵阳县| 松滋市|