0

I am getting an org.springframework.beans.factory.BeanCreationException in my spring controller as I use ResponseList bean.

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)

Here is the context part:

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

Here is the 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;
    }

}

Can anyone please correct what I am missing here?

user2918640
  • 473
  • 1
  • 7
  • 25

2 Answers2

1

using constructor injection

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

then bean like

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;
    }

}

and if setter injection

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

then bean like

public class ResponseList {

    public ResponseList() {
    }

    private List<Response> responseList;

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

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

}
Darshan Patel
  • 3,176
  • 6
  • 26
  • 49
  • So the `responseList` bean will include `response` bean object state automatically? – user2918640 Feb 03 '14 at 06:18
  • Now I am getting `org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.form.response.ResponseList' to required type 'java.util.List' for property 'responseList'` – user2918640 Feb 03 '14 at 06:23
  • I found my answer here: http://stackoverflow.com/questions/2416056/how-to-define-a-list-bean-in-spring – user2918640 Feb 03 '14 at 06:44
  • yes that is using property di you can do same with constructor di as you did in your code but your java class has no costructor with list argument that's why you get `1 constructor arguments specified but no matching constructor found in bean` – Darshan Patel Feb 03 '14 at 06:45
  • Okay, thanks for the answer. I was little weak in this place. – user2918640 Feb 03 '14 at 06:58
1

The exception says, Error creating bean with name 'responseList' defined in ServletContext resource, Spring not getting a argument-ed constructor in ResponseList bean..

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

Replace your no-arg constructor from

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

with

public ResponseList(List<Response> responseList){
    this.responseList = responseList;
}
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64