Johnmark
Johnmark

Reputation: 65

Property of 'java.util.ArrayList<java.lang.String>' type cannot be injected by 'List'

I use intelliJ IDEA 16.1 version to practice Spring framework.

Student Class

package com.tistory.johnmarc;
import java.util.ArrayList;

public class Student {

private String name;
private int studentId;
private String depart;
private ArrayList<String> lecture;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getStudentId() {
    return studentId;
}

public void setStudentId(int studentId) {
    this.studentId = studentId;
}

public String getDepart() {
    return depart;
}

public void setDepart(String depart) {
    this.depart = depart;
}

public ArrayList<String> getLecture() {
    return lecture;
}

public void setLecture(ArrayList<String> lecture) {
    this.lecture = lecture;
}

}

StudentInfo Class

 package com.tistory.johnmarc;

/**
 * Created by JJH on 2016-07-11.
 */
public class StudentInfo {
    private Student student;

    public void setStudent(Student student) {
        this.student = student;
    }
public void getStudentInfo(){
    System.out.println(student.getName());
    System.out.println(student.getStudentId());
    System.out.println(student.getDepart());
    System.out.println(student.getLecture());
    System.out.println("=========================");
}

}

spring configuration xml file

<bean id="student1" class="com.tistory.johnmarc.Student">
    <property name="name" value="장정현"/>
    <property name="studentId">
        <value>123</value>
    </property>
    <property name="depart">
        <value>software</value>
    </property>
    <property name="lecture" >
      <!-- Problem is there -->
        <list>
            <value>database</value>
            <value>OOAD</value>
        </list>
    </property>
</bean>

I wrote the above code.

Error Message is Property of 'java.util.ArrayList' type cannot be injected by 'List'

Any idea how to fix it?

Upvotes: 1

Views: 2557

Answers (2)

Trisha
Trisha

Reputation: 3931

Try the other way around - change your Student class to use List, not ArrayList. The Student class doesn't need to know the List implementation.

So

private ArrayList<String> lecture;

should be

private List<String> lecture;

Similarly the getter & setter need to change to:

public List<String> getLecture() {
    return lecture;
}

public void setLecture(List<String> lecture) {
    this.lecture = lecture;
}

Upvotes: 1

Mano ranjan J
Mano ranjan J

Reputation: 86

Please try this:

<bean id="student1" class="com.tistory.johnmarc.Student">
<property name="name" value="장정현"/>
<property name="studentId">
    <value>123</value>
</property>
<property name="depart">
    <value>software</value>
</property>
<property name="lecture" >
  <!-- Changes start -->
    <util:list list-class="java.util.ArrayList">
        <value>database</value>
        <value>OOAD</value>         
    </util:list>
  <!-- Changes end -->
</property>

Reference: Spring Lists proprties

Upvotes: 0

Related Questions