2017-01-05 18 views
-1

感謝:ありがとうございます。春が適切なコンストラクタを解決していません

問題の説明:

  1. 以下のコードを実行しようとしているときに、私は例外を直面しています。手がかりもない。
  2. @Autowiredを使用して依存クラスを開始するXML定義を記述する方法intパラメータを持つコンストラクタ。

ExceptionMessage:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dependent' defined in class path 
resource [simple-context.xml]: 
Could not resolve matching constructor (hint: specify index/type/name arguments 
for simple parameters to avoid type ambiguities) 

名:春のcontext.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:c="http://www.springframework.org/schema/c" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.learning.spring.constinjection" /> 

    <bean id="dependency" class="com.learning.spring.constinjection.Dependency"></bean> 

    <bean id="dependent" class="com.learning.spring.constinjection.Dependent"> 
     <constructor-arg ref="dependency" name="dpcy" /> 
     <constructor-arg name="mesg" value="Hi this is a test mesg" /> 
    </bean> 

    <bean id="dependent2" class="com.learning.spring.constinjection.Dependent" 
     c:mesg="Hi, this is another test mesg" /> 


</beans> 

名:Dependent.java

public class Dependent { 

    Dependency dpcy; 

    Dependent(Dependency dpcy, String mesg){ 
     this.dpcy = dpcy; 
     System.out.println("Message is: " + mesg); 
    } 

    Dependent(String mesg){ 
     System.out.println("Only Message is: " + mesg); 
    } 

    @Autowired 
    Dependent(Integer ticketno){ 
     System.out.println("Ticket no is: " + ticketno); 
    } 

} 

名:Dependency.java

public class Dependency { 

    Dependency(){ 
     System.out.println("Dependency loaded successfully"); 
    } 

} 

ファイル名:これまで

<bean id="dependent" class="com.learning.spring.constinjection.Dependent"> 
     <constructor-arg ref="dependency" name="dpcy" /> 
     <constructor-arg name="mesg" value="Hi this is a test mesg" /> 
    </bean> 

:ConstructorInjectionApp

public class ConstructorInjectionApp { 
     public static void main(String[] args) { 

     GenericXmlApplicationContext gen = new GenericXmlApplicationContext(); 
     gen.load("classpath:simplespringconstinjection.xml"); 
     gen.refresh(); 
     Dependent d = (Dependent) gen.getBean("dependent"); 
     Dependent d2 = (Dependent) gen.getBean("dependent2"); 
} 
} 
+1

'Dependent'コンストラクタで' @ Autowired'アノテーションが必要ですか? autowiredアノテーションとxml/java定義を混在させるのは一般的に混乱します。 – sisyphus

+0

それは混乱していますが、私は春を学んでいます。だから私は最初に物事を学び、ベストプラクティスに行くべきだと思った。 –

+0

私はdownvoteの理由を知っているかもしれませんか? –

答えて

0

この変更により、

<bean id="dependent" class="com.learning.spring.constinjection.Dependent" 
     c:dpcy-ref="dependency" c:mesg="Hi, this is another test mesg" /> 

を私はApplicationContextが正常にエラーなしで初期化するために取得することができました。

「c」名前空間については、this Spring docを参照してください。

まだ@Autowiredアノテーションを持つコンストラクタに問題があります。

+0

@SotiriosDelimanolisポインタをありがとうございます。私は "c"名前空間に慣れていませんでした。私はまた、私の元々の反応について、怠惰な仕事をしました。 – mangotang

関連する問題