0

I have the following in my properties file,

increments = 10,20,30

I want to read these properties in my POJO,

@Value("#{'${increments}'.split(',')}")
List<Integer> increments;

List<Integer> getIncrements(){
  return increments;
}

And then use this POJO in another class,

List<Integer> increments = pojo.getIncrements();

However, in the last call, I get a conversion error - String to Integer. To overcome this, I am reading the array as a String array, and converting it to Integer array explicitly in the getIncrements() method.

Is there a way to read an array of integers from a properties file into a POJO without explicit type conversion?

ufdeveloper
  • 387
  • 6
  • 19
  • 3
    Instead of `List` try `int[]` or `Integer[]` with just a `@Value("${increments"}`. Spring should be able to do the type conversion for you that way. – M. Deinum Jun 12 '14 at 05:45
  • What version of Spring are you using? I think that such issues were fixed in some minor iteration – geoand Jun 12 '14 at 05:49
  • @geoand I am using Spring 3.1.0 – ufdeveloper Jun 12 '14 at 06:05
  • 1
    @ufdeveloper Try upgrading to Spring 3.1.4. If that doesn't work and you have the ability to upgrade to Spring 3.2, then go for Spring 3.2.9 and let me know – geoand Jun 12 '14 at 06:07
  • @M.Deinum It works with your approach, but I still need to convert the array to a list explicitly. It is much cleaner than what I was doing though. I'll try geoand's suggestion to check if its possible to do it without explicit conversion. – ufdeveloper Jun 12 '14 at 18:00

0 Answers0