0

org.springframework.beans.factory.BeanCreationException当我使用 ResponseList bean 时,我在我的 spring 控制器中得到了一个。

Error creating bean with name 'responseList' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: 1 constructor arguments specified but no matching constructor found in bean 'responseList' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

这是上下文部分:

<bean id="response" class="com.form.response.Response" scope="prototype"></bean>    
    <bean id="myController" class="com.controllers.MyController" scope="prototype">
            <property name="response" ref="response"></property>
            <property name="responseList" ref="responseList"></property>
    </bean>
    <bean id="responseList" class="carey.form.response.ResponseList">
            <constructor-arg type="java.util.List">
                <list>
                   <ref bean="response"/>
                </list>
            </constructor-arg>
    </bean>

这是豆:

package com.form.response;

import java.util.List;

public class ResponseList {

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

    private List<Response> responseList;

    public List<Response> getResponseList() {
        return responseList;
    }

    public void setResponseList(List<Response> responseList) {
        this.responseList = responseList;
    }

}

谁能纠正我在这里遗漏的内容?

4

2 回答 2

1

使用构造函数注入

<bean id="response" class="com.form.response.Response" scope="prototype"></bean>    
    <bean id="myController" class="com.controllers.MyController" scope="prototype">
            <property name="response" ref="response"></property>
            <property name="responseList" ref="responseList"></property>
    </bean>
    <bean id="responseList" class="carey.form.response.ResponseList">
            <constructor-arg type="java.util.List">
                <list>
                   <ref bean="response"/>
                </list>
            </constructor-arg>
    </bean>

然后像豆子

package com.form.response;

import java.util.List;

public class ResponseList {

    public ResponseList(List<Response> responseList) {
           this.responseList=responseList;
        // TODO Auto-generated constructor stub
    }

    private List<Response> responseList;

    public List<Response> getResponseList() {
        return responseList;
    }

    public void setResponseList(List<Response> responseList) {
        this.responseList = responseList;
    }

}

如果设置器注入

<bean id="response" class="com.form.response.Response" scope="prototype"></bean>    
    <bean id="myController" class="com.controllers.MyController" scope="prototype">
            <property name="response" ref="response"></property>
            <property name="responseList" ref="responseList"></property>
    </bean>
    <bean id="responseList" class="carey.form.response.ResponseList">
            <property type="java.util.List">
                <list>
                   <ref bean="response"/>
                </list>
            </property>
    </bean>

然后像豆子

public class ResponseList {

    public ResponseList() {
    }

    private List<Response> responseList;

    public List<Response> getResponseList() {
        return responseList;
    }

    public void setResponseList(List<Response> responseList) {
        this.responseList = responseList;
    }

}
于 2014-02-03T06:14:44.107 回答
1

异常说,Error creating bean with name 'responseList' defined in ServletContext resourceSpring 没有在 ResponseList bean 中获得参数化构造函数。

<bean id="responseList" class="carey.form.response.ResponseList">
        <constructor-arg type="java.util.List">
            <list>
               <ref bean="response"/>
            </list>
        </constructor-arg>
</bean>

替换您的无参数构造函数

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

public ResponseList(List<Response> responseList){
    this.responseList = responseList;
}
于 2014-02-03T06:47:25.777 回答