0

Is there any way to load a properties file in spring in order ? I understand Properties is a Hashtable and maps are unordered. I would like a xml based solution rather than a java based solution. Ideally, this should be configurtable from outside.

Edit:
I mean the contents of the properties file should be read and preserved in the same order as in the properties file. Eg:
fr.wiki=http://fr.wikipedia.org
en.wiki=http://en.wikipedia.org
...

If I read the properties file, fr.wiki should be first, en.wiki should be second and so on.
bsd
  • 2,707
  • 1
  • 17
  • 24
  • r u using `PropertyPlaceHolder` ? If so check [this](http://stackoverflow.com/questions/14192373/what-is-property-resolution-order-in-spring-property-placeholder-configurer-with) – Sridhar Jun 07 '13 at 06:14
  • 4
    What is your use case? Why do you need ordered properties? –  Jun 07 '13 at 06:42
  • I did give an example. Let's say in the end you have *.wiki=http://en.wikipedia.org . I am using for internationalization. The first part may be a basic regex. I don't want a broader regex to match before a specific regex. – bsd Jun 07 '13 at 06:46

1 Answers1

0

Getting properties in the order they are written in the properties file is not possible. You can add numbers to your property names:

component.01.key = value1
component.02.key = value2
component.03.key = value3
...

See Pulling values from a Java Properties file in order?

Community
  • 1
  • 1
Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
  • 1
    Very poor solution. Requires internal code changes, makes it harder to read, reason about and maintain. – bsd Jun 10 '13 at 10:01
  • Well, this is the best you can get with `java.util.Properties`. You just iterate over `getPropertyNames()`, extract properties with defined prefix and then sort them to get your ordered values. – Pavel Horal Jun 10 '13 at 10:10
  • Well, do you really think, I would have asked this question if I had to sort by keys ? All lines in properties file cannot follow component.numeric format, many of them contain numeric characters and dots. Besides component name is a "variable". The component name may itself need sorting. This adds a lot of complexity to the parsing logic, akin to creating a new file format which no one is likely to use(ever). – bsd Jun 10 '13 at 11:27
  • With this I have to admit I absolutely don't understand your use case, nor your comment. I thought that if you had `key1=val1` you just can prefix it with something like `foo.01.key1=val1` so that you are able to use alphanumeric sortin on the keys. I didn't propose new file format. – Pavel Horal Jun 10 '13 at 11:35
  • You may be happy with your solution but I am not. – bsd Jun 12 '13 at 13:09