Java 指定了1个构造函数参数,但在bean中找不到匹配的构造函数,为什么auto wire byname不';无法使用构造函数arg

Java 指定了1个构造函数参数,但在bean中找不到匹配的构造函数,为什么auto wire byname不';无法使用构造函数arg,java,spring,Java,Spring,如何设置emp Id及其地址,我还想保留地址的autowire=“byName”。 请避免使用 下面是我的情景 spring config.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="h

如何设置emp Id及其地址,我还想保留地址的autowire=“byName”。

请避免使用

下面是我的情景

spring config.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <bean id="empBean" class="com.spring.SpringCoreIOC.autowiring.Employee"
        autowire="byName">
        <constructor-arg index="0" type="int" value="11111" />
    </bean>

    <bean id="address" class="com.spring.SpringCoreIOC.autowiring.Address">
        <constructor-arg value="Secret Chowk" />
    </bean>
</beans>  
Employee.java,类有自己的id、int类型和地址类型

public class Employee {

    private Address address;
    private int id;

    public Employee(int id, Address address) {
        super();
        this.id = id;
        this.address = address;
    } }
我面临的错误如下所示

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empBean' defined in class path resource [autowiring_byNameByTag.xml]: 1 constructor arguments specified but no matching constructor found in bean 'empBean' (hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)
提前谢谢

如果您使用
autowire=“byName”
并希望将
id
参数传递给构造函数,那么您应该使用仅具有
id
参数的构造函数:
public Employee(int id)
,并通过setter设置您的地址:

public class Employee {

    private Address address;
    private int id;

    public Employee(int id) {
        super();
        this.id = id;
    } 

    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
}
因为
autowire=“byName”
不适用于构造函数参数,但
autowire=“constructor”
适用于构造函数参数。

如果您使用
autowire=“byName”
并希望将
id
参数传递给构造函数,那么您应该使用仅具有
id
参数的构造函数:
public Employee(int id)
并通过setter设置您的地址:

public class Employee {

    private Address address;
    private int id;

    public Employee(int id) {
        super();
        this.id = id;
    } 

    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
}
因为
autowire=“byName”
不能使用构造函数参数,但是
autowire=“constructor”
可以使用构造函数参数。

有关详细信息: 我想通过Ivan,Kirill提到的链接反映我的理解

在这种情况下,它是在看学生上课 属性,如id=“address”,因为地址是学生的属性

<bean id="studentBean" class="com.lokesh.SpringCore.entity.Student" autowire="byName"/>
<bean id="address" class="com.lokesh.SpringCore.entity.Address">
        <constructor-arg value="EARTH" />
</bean>

它正在考虑构造函数参数, 财产并不重要


请纠正我或补充你的观点,如果我没有涵盖任何方面

谢谢

更多信息: 我想通过Ivan,Kirill提到的链接反映我的理解

在这种情况下,它是在看学生上课 属性,如id=“address”,因为地址是学生的属性

<bean id="studentBean" class="com.lokesh.SpringCore.entity.Student" autowire="byName"/>
<bean id="address" class="com.lokesh.SpringCore.entity.Address">
        <constructor-arg value="EARTH" />
</bean>

它正在考虑构造函数参数, 财产并不重要


请纠正我或补充你的观点,如果我没有涵盖任何方面


谢谢

用示例检查此链接看起来您需要为
员工设置autowire=“constructor”
beangreat谢谢lvan,明白了。我可以知道为什么autowire=“byName”不能使用构造函数参数吗?用示例检查此链接看起来您需要为
员工设置autowire=“constructor”
beangreat谢谢lvan,明白了。我可以知道为什么yautowire=“byName”不适用于构造函数参数吗?谢谢你的指导,基里尔·西蒙诺夫!,我可以知道为什么yautowire=“byName”不能使用构造函数参数吗?@Lokesh,因为当按名称自动连接时,spring会查看属性名称。实际上,所有自动布线模式(除了
构造函数
)都使用属性。看看“洛克斯”绝对正确。还要注意的是,当在构造函数模式下自动连接时,spring会查看参数类型,而不是名称。如果容器中没有一个构造函数参数类型的bean,则会引发一个致命错误。正确,我已将其格式化为新答案,以便其他人将来不会混淆!!谢谢你的指导,基里尔·西蒙诺夫!,我可以知道为什么yautowire=“byName”不能使用构造函数参数吗?@Lokesh,因为当按名称自动连接时,spring会查看属性名称。实际上,所有自动布线模式(除了
构造函数
)都使用属性。看看“洛克斯”绝对正确。还要注意的是,当在构造函数模式下自动连接时,spring会查看参数类型,而不是名称。如果容器中没有一个构造函数参数类型的bean,则会引发一个致命错误。正确,我已将其格式化为新答案,以便其他人将来不会混淆!!