私は春が初めてです。以下のように、1 つの Bean クラスと構成ファイルを作成しました。
Beans.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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employee" class="com.asd.bean.Employee">
<constructor-arg index="0" type="java.lang.String" value="kuldeep" />
<constructor-arg index="1" type="java.lang.String" value="1234567" />
</bean>
</beans>
従業員.java
package com.asd.bean;
public class Employee {
private String name;
private String empId;
public Employee() {
System.out.println("Employee no-args constructor");
}
Employee(String name, String empId)
{
System.out.println("Employee 2-args constructor");
this.name=name;
this.empId=empId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the empId
*/
public String getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(String empId) {
this.empId = empId;
}
public String toString() {
return "Name : "+name+"\nEID : "+empId;
}
}
ApplicationContext を使用して Bean を取得しようとすると、次の例外が発生します。
スレッド「メイン」での例外 org.springframework.beans.factory.BeanCreationException: クラスパスリソースで定義された名前「employee」を持つ Bean の作成中にエラーが発生しました [Problem.xml]: 2 つのコンストラクター引数が指定されましたが、一致するコンストラクターが Bean「employee」に見つかりませんでした (ヒント: 型のあいまいさを避けるために、単純なパラメーターにはインデックス/型/名前の引数を指定してください)
デフォルトのコンストラクターからパブリックを削除すると、正常に機能し、両方のコンストラクターをパブリックにする場合でも機能します。この動作を示している理由を説明してください???
前もって感謝します。