[英]Spring autowire by constructor
我有兩個類 Test 和 SampleClassWithMultipleProperty ,它們的 bean 定義如下:
<bean name="test" class="Test" autowire="constructor">
</bean>
<bean id="instance1" class="SampleClassWithMultipleProperty">
<constructor-arg type="java.lang.String" value="constructor-arg-1"/>
<constructor-arg type="java.lang.String" value="constructor-arg-2"/>
<constructor-arg type="java.lang.String" value="constructor-arg-3"/>
<constructor-arg type="int" value="4"/>
<constructor-arg type="java.lang.Integer" value="5"/>
</bean>
當我指定任何名稱 id 時,例如 "instance1" 、"instance2"、"instance3" 它可以工作。
但是當我有另一個相同類型的 bean 時 SampleClassWithMultipleProperty 如下:
<bean id="instance2" class="SampleClassWithMultipleProperty">
<constructor-arg type="java.lang.String" value="constructor-arg-1-ind"/>
<constructor-arg type="java.lang.String" value="constructor-arg-2-ind"/>
<constructor-arg type="java.lang.String" value="constructor-arg-3-ind"/>
<constructor-arg type="int" value="4"/>
<constructor-arg type="java.lang.Integer" value="5"/>
</bean>
容器無法在 Test 類(具有關系)中初始化 SampleClassWithMultipleProperty 的 bean 實例(名稱為 sampleClassWithMultipleProperty)。
當我將 bean 的任何 id 從 "instance1" 更改為 "sampleClassWithMultipleProperty" OR "instance2" 到 "sampleClassWithMultipleProperty" 時,它成功地做到了這一點。
但是為什么會這樣。 我在某處讀到通過構造函數自動裝配類似於按類型自動裝配。 所以理想情況下它應該匹配 bean 的類型,即類名。
請在下面找到我的課程:
public class Test {
SampleClassWithMultipleProperty sampleClassWithMultipleProperty;
public Test() {
super();
// TODO Auto-generated constructor stub
}
public Test(SampleClassWithMultipleProperty sampleClassWithMultipleProperty) {
super();
System.out.println("in Test constructor");
this.sampleClassWithMultipleProperty = sampleClassWithMultipleProperty;
}
public SampleClassWithMultipleProperty getSampleClassWithMultipleProperty() {
return sampleClassWithMultipleProperty;
}
public void setSampleClassWithMultipleProperty(
SampleClassWithMultipleProperty sampleClassWithMultipleProperty) {
this.sampleClassWithMultipleProperty = sampleClassWithMultipleProperty;
}
}
public class SampleClassWithMultipleProperty {
private String property1;
private String property2;
private String property3;
private int property4;
private Integer property5;
public SampleClassWithMultipleProperty() {
super();
// TODO Auto-generated constructor stub
}
public SampleClassWithMultipleProperty(String property1, String property2, String property3,
int property4, Integer property5) {
super();
this.property1 = property1;
this.property2 = property2;
this.property3 = property3;
this.property4 = property4;
this.property5 = property5;
}
public String getProperty1() {
return property1;
}
public void setProperty1(String property1) {
this.property1 = property1;
}
public String getProperty2() {
return property2;
}
public void setProperty2(String property2) {
this.property2 = property2;
}
public String getProperty3() {
return property3;
}
public void setProperty3(String property3) {
this.property3 = property3;
}
public int getProperty4() {
return property4;
}
public void setProperty4(int property4) {
this.property4 = property4;
}
public Integer getProperty5() {
return property5;
}
public void setProperty5(Integer property5) {
this.property5 = property5;
}
}
嗨,我不是彈簧專家,但我知道一些細節並進行了一些搜索,我想在此基礎上回答。
我假設你有默認自動裝配的 id。 您有兩個類 SampleClassWithMultipleProperty 的 bean 定義。 這兩者都有資格創建並用作測試的構造函數 arg。
默認情況下,自動裝配掃描並匹配范圍內的所有 bean 定義。 如果你想排除一些 bean 定義,以便它們不能通過自動裝配模式注入,你可以使用設置為 false 的autowire-candidate
來做到這一點。
所以你可以做的是:
<bean id="instance2" class="SampleClassWithMultipleProperty" autowire-candidate="false">
您還可以選擇使用default-autowire-candidates
屬性,這應該可以幫助您。 您可以只傳遞一個模式,該模式將用於掃描 bean 創建的可能候選者。
您的Test
bean 有 2 個構造函數:一個無參數構造函數和一個需要SampleClassWithMultipleProperty
實例的構造函數。
當您的類路徑中只有一個SampleClassWithMultipleProperty
實例時,spring 只會選擇采用SampleClassWithMultipleProperty
的構造函數。
當您有兩個SampleClassWithMultipleProperty
spring 將無法在它們之間進行選擇,因此將選擇 no arg 構造函數。
當您更改與構造函數參數名稱匹配的SampleClassWithMultipleProperty
bean 之一的id
,spring 將能夠在它們之間進行選擇,並且能夠使用期望SampleClassWithMultipleProperty
實例的構造函數創建您的 bean。
如果在第二種情況下(2 個SampleClassWithMultipleProperty
bean 並且沒有與參數名稱匹配的 bean),您將有一個NoUniqueBeanDefinitionException
因為 spring 將無法使用正確的構造函數來構建您的 bean。
Spring 會自動選擇滿足盡可能多參數的構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.