gpt4 book ai didi

java - Spring创建bean时出错

转载 作者:行者123 更新时间:2023-12-02 04:50:56 25 4
gpt4 key购买 nike

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

"创建类路径资源 [org/collection/ApplicationContext2.xml] 中定义的名为“question”的 bean 时出错:指定了 3 个构造函数参数,但在 bean“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

<?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>

最佳答案

Question中的三参数构造函数期待ListString 。但是,你正在传递ListAnswer 。更改 Question 中的第三个参数类(class) ArrayList<Answer> answers

关于java - Spring创建bean时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29249272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com