spring注入的幾種方式

2021-08-26 19:08:39 字數 1038 閱讀 8333

1、setter方式(最常用)

就是在bean中的屬性必須有setter方法 然後在xml在配置

2、構造方法(不常用)

如果構造方法需要多個引數 則可以按照引數索引賦值或型別賦值(引數中沒有相同型別 )

a.按引數型別傳給構造方法注入[ constructor argument type matching ]

b.按引數索引傳給構造方法注入(索引從0開始)[ constructor argument index ]

some examples

public class examplebean 

public void setbeantwo(yetanotherbean beantwo)

public void setintegerproperty(int i)

}

public class examplebean 

}

public class examplebean 

// a static factory method; the arguments to this method can be

// considered the dependencies of the bean that is returned,

// regardless of how those arguments are actually used.

public static examplebean createinstance (

anotherbean anotherbean, yetanotherbean yetanotherbean, int i)

}

3、介面注入(不常用)

網上說會增加耦合 不建議使用 我也沒有用過 暫且先不談 會了的話在更新^_^

Spring注入的方式

spring依賴注入分為兩種 建構函式注入和setter方法注入 1 建構函式注入的配置檔案如下 i am a learner.helloworld.properties 在上面的這個配置檔案中我們主要有3個類 com.openv.impl.userinfoimpl,com.openv.impl.h...

spring注入方式

一 二 三種注入方式 1.通過set方法賦值 賦值為物件2.通過構造器賦值 3.通過p標籤賦值 當屬性值的型別相同時,常需要type來區分 注意 必須提供無參構造給bean4.自動裝配 自動尋找beanid 類的屬性值 自動尋找其他bean的型別 class 是否與該類的ref屬性型別一致 是否與該...

spring 注入方式

第一種 構造器注入 第二種 set依賴注入 bean id address class com.kuang.pojo.address property name address value 西安 property bean bean id student class com.kuang.pojo.s...