Spring自動裝配Beans

2022-09-13 14:00:15 字數 4023 閱讀 7398

自動裝配(autowiring),我是這樣理解的:bean1作為bean2的屬性,在配置檔案中設定二者的關聯後,當spring容器建立bean2物件時,會自動建立bean1的物件並賦值給bean1物件的相關屬性。

1. 自動裝配模式

自動裝配模式有五種,通過在中定義autowire屬性進行選擇。

① 不定義該屬性:即通過 ref 屬性進行手動設定。

② autowire="byname"

③ autowire="bytype"

④ autowire="constructor"

⑤ autowire="autodetect"

2. 使用註解自動注入: @autowired,通過匹配資料型別自動裝配bean

無需在配置檔案中設定關聯,僅需在主動引用方的類中,在需要自動裝配的位置新增 @autowired 即可。

可以新增註解的位置有三種:setter方法,建構函式,字段(即屬性)。

如果在配置檔案中沒有找到相應的bean,自動裝配顯然會出錯,這是因為 @autowired 預設執行依賴檢查,當自動裝配失敗時,會丟擲異常。

如果不想檢查直接裝配,可手動設定 @autowired(required=false),requeired屬性預設為true,表示是否需要執行依賴檢查。(不執行依賴檢查,切裝配失敗時,相應屬性不予設定)

/**

* @autowired setter方法

*/package

com.yiibai.common;

import

org.springframework.beans.factory.annotation.autowired;

public

class

customer

}

/**

* @autowired 構造方法

*/package

com.yiibai.common;

import

org.springframework.beans.factory.annotation.autowired;

public

class

customer

}

/**

* @autowired 字段

*/package

com.yiibai.common;

import

org.springframework.beans.factory.annotation.autowired;

public

class

customer

/**

* 測試

*/package

com.yiibai.common;

import

import

public

class

}

注意:要想啟用@autowired,必須先註冊autowiredannotationbeanpostprocessor。

註冊方式有兩種:

①直接在配置檔案中包含『aabpp』,即新增下行**:

<

beans

xmlns

=""xmlns:xsi

=""xsi:schemalocation

="/spring-beans-2.5.xsd"

>

<

bean

id="customerbean"

class

="com.yiibai.common.customer"

>

<

property

name

="action"

value

="buy"

/>

<

property

name

="type"

value

="1"

/>

bean

>

<

bean

id="personbean"

class

="com.yiibai.common.person"

>

<

property

name

="name"

value

="yiibai"

/>

<

property

name

="address"

value

="address abc"

/>

<

property

name

="age"

value

="29"

/>

bean

>

beans

>

② include

<

beans

xmlns

=""xmlns:xsi

=""xmlns:context=""

xsi:schemalocation

="/spring-beans-2.5.xsd

/spring-context-2.5.xsd"

>

<

bean

id="customerbean"

class

="com.yiibai.common.customer"

>

<

property

name

="action"

value

="buy"

/>

<

property

name

="type"

value

="1"

/>

bean

>

<

bean

id="personbean"

class

="com.yiibai.common.person"

>

<

property

name

="name"

value

="yiibai"

/>

<

property

name

="address"

value

="address abc"

/>

<

property

name

="age"

value

="29"

/>

bean

>

beans

>

3. @qualifier,通過匹配bean的id,對bean進行自動裝配

由於 @autowired 是通過匹配資料型別來自動裝配bean,所有當被裝配的實體類有具多個bean時,@autowired 是無法得知裝配哪個bean的。

這時就要使用@qualifier,較之匹配資料型別的 @autowired,@qualifier是對節點的 id 進行匹配。

package

com.yiibai.common;

import

org.springframework.beans.factory.annotation.autowired;

import

org.springframework.beans.factory.annotation.qualifier;

public

class

customer

Spring自動裝配Beans

在spring框架,可以用 auto wiring 功能會自動裝配bean。要啟用它,只需要在 定義 autowire 屬性。在spring中,支援 4 種自動裝配模式。這是預設的模式,你需要通過 ref 屬性來連線 bean。按屬性名稱自動裝配。在這種情況下,由於對 person bean的名稱是...

Spring自動裝配Beans

在spring框架,可以用 auto wiring 功能會自動裝配bean。要啟用它,只需要在 定義 autowire 屬性。在spring中,支援 5 自動裝配模式。customer 和 person 物件自動裝配示範。package com.yiibai.common public class ...

Spring自動裝配Beans

在spring框架,可以用 auto wiring 功能會自動裝配bean。要啟用它,只需要在 定義 autowire 屬性。在spring中,支援 5 自動裝配模式。customer 和 person 物件自動裝配示範。package com.yiibai.common public class ...