2016-06-28 16 views
-3
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> 
+0

私のようにすべてをチェックしますjarファイル、構文 –

+1

例外は何が間違っているかを正確に伝えています... 2つのパラメータを持つコンストラクタはありません...文字列またはintを取る2つのコンストラクタがあります。 –

+0

あなたはどう思いますか?一致するコンストラクタ_を解決できませんでしたか? –

答えて

1

あなたTestクラスは、唯一の引数なしのコンストラクタを持っていますインスタンスは、Stringintをパラメータとして受け入れるコンストラクタを呼び出すことによって構築する必要があります。

あなたTestクラスのコンストラクタを追加することができ、次のいずれか

public class Test { 
    private String name; 
    private int age; 
    public Test(String name, int age) { 
     this.name = name; 
     this.age = age; 
    } 
    (...) 
} 

またはコンストラクタ、セッターを経由して値を注入するために、あなたのSpring構成ファイルではなく変更:

<bean id="t" class="beans.Test">  
    <property name="name" value="vikram"/>  
    <property name="age" value="123"/> 
</bean> 
関連する問題