package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Test;
public class Client {
public static void main(String args[]) {
ApplicationContext ap=new ClassPathXmlApplicationContext("res/spring.xml");
Test t=(Test)ap.getBean("t");
t.printData();
}
}
-----------------------------------どのように春のフレームワークでBeanの例外を解決するには?
package beans;
public class Test {
private String name;
private int age;
public Test(String name) {
this.name=name;
}
public Test(int age) {
this.age=age;
}
public void printData() {
System.out.println("age="+age);
System.out.println("name="+name);
}
}
--- ---------------------------------
2016年6月28日4:01:06 PM .springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO:リフレッシュするorg[email protected]138eb89:起動日[Tue Jun 28 16:01:06 IST 2016];コンテキスト階層のルート
2016年6月28日午前4時01分06秒PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO:クラスパスリソースからのロードXMLのBean定義[RES/spring.xml]
2016年6月28日4:01:06 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO:org.s[email protected]616ca2でのシングルトンの事前インスタンス化:beanの定義[t ];工場階層のルート
2016年6月28日午前4時01分06秒PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons INFO:org.s[email protected]616ca2でシングルトンを破棄:豆を定義する[t];ファクトリ階層のルート スレッド "main"の例外org.springframework.beans.factory.BeanCreationException:クラスパスresource [res/spring.xml]で定義された名前 't'のBeanを作成中にエラーが発生しました:一致するコンストラクタを解決できませんでした(ヒント:型のあいまいさを避けるための簡単なパラメータのインデックス/型/名前引数の指定** **
--------------------------- -------- XML設定ファイルで、あなたはことを指定するのに対し、
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd
(http://www.springframework.org/dtd/spring-beans-2.0.dtd)">
<beans>
<bean id="t" class="beans.Test">
<constructor-arg value="vikram" type="java.Lang.String" index="0"/>
<constructor-arg value="123" type="int" index="1"/>
</bean>
</beans>
私のようにすべてをチェックしますjarファイル、構文 –
例外は何が間違っているかを正確に伝えています... 2つのパラメータを持つコンストラクタはありません...文字列またはintを取る2つのコンストラクタがあります。 –
あなたはどう思いますか?一致するコンストラクタ_を解決できませんでしたか? –