Search code examples
spring-bootproperty-files

How to read array representation from property file in spring boot


I would like to read a property file which has array format.

I have this property file:

students.student[0] = ABC
students.student[0].marks[0] = 10
students.student[0].marks[1] = 20
students.student[0].marks[2] = 30
students.student[0].marks[3] = 40
students.student[0].marks[4] = 50
students.student[0].marks[5] = 60
students.student[0].marks[6] = 70

students.student[1] = XYZ
students.student[1].marks[0] = 10
students.student[1].marks[1] = 20
students.student[1].marks[2] = 30
students.student[1].marks[3] = 40
students.student[1].marks[4] = 50
students.student[1].marks[5] = 60
students.student[1].marks[6] = 70

I am trying to read this property file as follow:

@ConfigurationProperties("students")
@PropertySource("classpath:student.properties")
public class StudentProperties{
   List<Student> students = new ArryaList<Student>();
   public static class Student
   {
      String name;
      List<Marks> marks = new ArrayList<Marks>;
      public static class Marks
      {
         int marks;
          //getters and setters
      }     
      //getters and setters
   }
   //getters and setters
 }

But It is not setting values. How to do this?


Solution

  • Mapping POJO is not in correct format, In Student class Just have List<Integer>

    @ConfigurationProperties("students")
    @PropertySource("classpath:student.properties")
    public class StudentProperties{
    List<Student> student = new ArryaList<Student>();
       public static class Student
          {
             String name;
             List<Integer> marks = new ArrayList<Integer>();
    
            //getters and setters
         }
         //getters and setters
      }