在Spring中创建bean时出错

时间:2015-03-25 06:54:50

标签: java spring

大家好,我是春天的初学者,我刚刚开始。 我收到了错误

"创建名称为'问题'在类路径资源[org / collection / ApplicationContext2.xml]中定义:指定了3个构造函数参数,但在bean' question'中找不到匹配的构造函数。 (提示:为简单参数指定索引/类型/名称参数以避免类型歧义)"

我有两个类1st是包含单个构造函数的Question而第二个类是Answer

我正在尝试创建答案类的引用并插入到具有Array List

的Question类中

我g目结舌,发现我需要指定类型。 我已经指定了它,但我仍然收到错误

谢谢..

Question.java

package org.collection;

import java.awt.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;



public class Question {
    private int id;
    private String name;
    private ArrayList<String> answers;
    //private HashSet<String> answers1;

    public Question()
    {
        //Default constructor
    }

    public Question(int id,String name,ArrayList<String> answers)
    {
        super();
        this.id=id;
        this.name=name;
        this.answers=answers;

    }

    public void display()
    {
        System.out.println("Id :"+id+"\nName :"+name);
        System.out.println("Answers are");
        Iterator<String> itr= answers.iterator();  
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }

        /*System.out.println("----------picking up the answers from HashSet---------");
         Iterator<String> itr1=answers1.iterator();
         while(itr1.hasNext())
         {
             System.out.println(itr1.next());
         }
         System.out.println("-------reached-----------");*/



    }

}

Answer.java

package org.collection;

public class Answer {
    private int id;
    private String name;
    private String by;

    public Answer() {
        // TODO Auto-generated constructor stub
    }

    public Answer(int id,String name,String by)
    {
        super();
        this.id=id;
        this.name=name;
        this.by=by;

    }

    public String toString()
    {
        return "ID :"+id+"\nName"+name+"\nBy :"+by;
    }
}

ApplicationContext.xml2

&#13;
&#13;
<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 

 <bean id="ans1" class="org.collection.Answer">
 <constructor-arg value="1" type="int"></constructor-arg>
 <constructor-arg value="java is a progamming language hahahaha"  type="java.lang.String"></constructor-arg>
 <constructor-arg value ="varun"  type="java.lang.String"> </constructor-arg>
 </bean>
 
 <bean id ="ans2" class="org.collection.Answer">
 <constructor-arg value="2" type="int"></constructor-arg>
 <constructor-arg value="java is a platfornm"  type="java.lang.String"></constructor-arg>
 <constructor-arg value ="Rahul"  type="java.lang.String"></constructor-arg>
 </bean>
  
 <bean id="question" class= "org.collection.Question">
 <constructor-arg value="111" type="int"></constructor-arg>
 <constructor-arg value="What is java ?" type="java.lang.String"></constructor-arg>
 <constructor-arg>
 <list>
 <ref bean="ans1"/>
<ref bean="ans2"/> 
 </list>
  </constructor-arg>
  </bean>
 </beans>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

Question中的三个参数构造函数期待List String。但是,您正在通过List的{​​{1}}。将Answer类中的第三个参数更改为Question