1

I have a property file Details2.txt and it is filled with some values like :

list.value=34
list.value=35
list.value=38
list.value=45
list.value=23

Now i want to iterate this in my spring xml and i am unable to do so

XML code (parts only) :

 <context:property-placeholder location="Details2.txt" />

    <constructor-arg>
       <list>
           <value>${list.value}</value>
       </list> 
    </constructor-arg>

this gives only the last value in property file

Amol
  • 303
  • 4
  • 13

1 Answers1

2

A property file will be translated to a Properties object, which in turn is a subclass of Hashtable, which contains a one-to-one mapping from key to value. This means that the Properties object will indeed only contain the last value (since the previous ones will have been overwritten).

One common way of handing this problem in property files is to add a suffix to the keys:

list.value.1=34
list.value.2=35
list.value.3=36
list.value.4=37

but that doesn't help you when using Spring, since each property key will need to be explicitly specified in the XML file (and I'm assuming you don't know the exact number of items).

Your best bet would be to specify the items as a comma-separated list:

list.values=34,35,36,37

And then use a configuration trick, such as described here or here.

Community
  • 1
  • 1
marthursson
  • 3,242
  • 1
  • 18
  • 28