Wie kann ich emp-ID sowie seine Adresse einstellen, und ich möchte auch autowire = " byName "für Adresse.1 Konstruktorargumente angegeben, aber kein übereinstimmender Konstruktor in Bean gefunden. Warum funktioniert der automatische Byname nicht mit Konstruktor-Arg?
Bitte Beantwortungs vermeiden, indem
<constructor-arg ref="address">
Unten ist mein Szenario
feder 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>
Address.java
public class Address {
private String place;
public Address(String place) {
super();
this.place = place;
}
public String toString() {
return place;
}
Employee.java, Klasse hat ihre eigene ID, int-Typ und Adresse Adresstyp
public class Employee {
private Address address;
private int id;
public Employee(int id, Address address) {
super();
this.id = id;
this.address = address;
} }
Fehler Ich bin vor wie unten
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)
Vielen Dank im Voraus !!
Überprüfen Sie diesen Link mit Beispiel https://www.concretepage.com/spring/spring-bean-autowire-byname-bytype-constructor-and-default-example#constructor Sieht so aus, als müssten Sie autowire = "constructor "für' Angestellte' Bohne – Ivan
großer Dank lvan, Got it. –
Darf ich wissen warum whyautowire = "byName" funktioniert nicht mit Konstruktorargumenten? –