なぜ曖昧とき、私は複数のコンストラクタの引数を注入?

アラン・フー:

私は2つのコンストラクタは、パラメータを参照しているの一つであり、別のプリミティブのパラメータを定義するときに、なぜこれが起こるのでしょうか?

以下は関連するスニペットです。

コンストラクタ1:

public TextEditor(SpellChecker sc) {...}

コンストラクタ2:

public TextEditor(int editorNum) {...}

Bean定義のXMLファイル:

...
<constructor-arg ref="spellChecker"/> <!--spellChecker is defined as class elsewhere-->
<constructor-arg value="100"/>
...

あいまいさのエラーをコンパイルすると、私はタグにタイプ/名前/インデックスを追加しても残っています。

以下の完全なスタックトレースを投稿

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditor' defined in class path resource [Beans.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tutorialspoint.MainApp.main(MainApp.java:7)
アニッシュB.:

適切なコンストラクタ・インジェクションは、 applicationContext.xml このようになります

例えば:あなたの仮定 TextEditorSpellChecker クラスがパッケージにあります com.test

さて、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">

    <!-- Create a SpellChecker bean -->
    <bean id = "spellChecker" class="com.test.SpellChecker"/>

    <!-- Create a TextEditor bean by calling the first constructor -->
    <bean id="textEditor" class="com.test.TextEditor" >
        <constructor-arg ref="spellChecker"/>
    </bean>

    <!-- Create a TextEditor bean by calling the second constructor -->
    <bean id="textEditor_1" class="com.test.TextEditor" >
        <constructor-arg value="100"/>
    </bean>

</beans>

これがあなたのお役に立てば幸いです:)

ありがとうございました。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=312174&siteId=1
おすすめ