gpt4 book ai didi

java - 构造函数的 Spring Autowiring

转载 作者:行者123 更新时间:2023-11-30 08:46:22 25 4
gpt4 key购买 nike

我有两个类 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”,它会起作用。

但是当我有另一个相同类型 SampleClassWithMultipleProperty 的 bean 时,如下所示:

<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>

容器无法在测试类(具有关联)中初始化 SampleClassWithMultipleProperty(名称为 sampleClassWithMultipleProperty)的 bean 实例。

当我将 bean 的任何 id 从“instance1”更改为“sampleClassWithMultipleProperty”或将“instance2”更改为“sampleClassWithMultipleProperty”时,它成功地做到了这一点。
但为什么它会这样。我在某处读到构造函数 Autowiring 类似于按类型 Autowiring 。所以理想情况下它应该匹配 bean 的类型,即类名。

请在下面找到我的类(class):

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;
}

}

最佳答案

您好,我不是 Spring 专家,但我知道一些知识并进行了一些搜索,我想在此基础上回答。

我假设您通过 id 设置了默认 Autowiring 。您有两个类 SampleClassWithMultipleProperty 的 bean 定义。这两者都有资格创建并用作测试的构造函数参数。

默认情况下, Autowiring 会扫描并匹配范围内的所有 bean 定义。如果你想排除一些 bean 定义,这样它们就不能通过 Autowiring 模式注入(inject),你可以使用 autowire-candidate 设置为 false 来做到这一点。
所以你可以做的是:

<bean id="instance2" class="SampleClassWithMultipleProperty"  autowire-candidate="false">

您还可以选择使用 default-autowire-candidates 属性,这应该可以帮助您解决问题。您可以只传递一个模式,该模式将用于扫描创建 bean 的可能候选对象。

Reference Link 1
Reference Link 2
Reference Link 3

关于java - 构造函数的 Spring Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32887215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com