Load properties file in spring in order - java

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.

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?

Related

Unable to read the last duplicate key of a Properties file

I am trying to read the last value corresponding to a duplicate key in my mail.properties file. The properties file body looks like :
//other key/values properties
mail.body=body1
mail.subject=Reminder
mail.body=body2
mail.body=body3
//other key/values properties
The mail.properties is a result of the concatenation of many properties files at build time. The present evolution requires that I read the last mail.subject and mail.body key/value entries. Thus the normal behavior of my resourceBundle.getString("mail.body") should return body3 but it returns body2 instead. I have double checked if I am reading the right file and it seems that everything is correct. What could be the reason behind this abnormal behavior ?

How to validate values in a YAML configuration file while loading it?

Is there a way to validate values in a YAML file while loading it in the code. The requirement is I have some elements in the YAML file which must have values. If the validation fails, then YAML should not be loaded.
I'm using snakeyaml library and heard there is a way to do this via Representer.
Code I'm currently using to load the YAML,
Reader in = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8);
Yaml yaml = new Yaml();
yaml.setBeanAccess(BeanAccess.FIELD);
return yaml.loadAs(in, School.class);
Since you can have any value in a YAML file, you should load the file in a function, test the values and raise an error if the values are not what you want. Return the loaded data if they are.
This may have side-effects if your YAML has tags that create arbitrary objects, but checking during loading will not prevent that, as such object might have been created before you come to the value you want to check.
If you do have tags in your YAML and that is a real problem, then you would have to make a safe_load-er for the YAML file that can handle the tags (by creating normal mapping objects), then check the values and reload with full tag support.

Which method I can use in Java for configuration file?

Which method I can use in Java for configuration files, to read and write specific lines and strings? For example (config.ini):
mysql_host: localhost
mysql_user: user
or
mysql_host = localhost
mysql_user = user
You can use ini4j library.
Ini ini = new Ini(new File(filename));
java.util.prefs.Preferences prefs = new IniPreferences(ini);
And then you can go ahead fetch nodes with prefs.node("mysql_host").
Have a look at Java Properties:
Properties are configuration values managed as key/value pairs. In
each pair, the key and value are both String values. The key
identifies, and is used to retrieve, the value, much as a variable
name is used to retrieve the variable's value. For example, an
application capable of downloading files might use a property named
"download.lastDirectory" to keep track of the directory used for the
last download.
To manage properties, create instances of java.util.Properties. This
class provides methods for the following:
loading key/value pairs into a Properties object from a stream,
retrieving a value from its key,
listing the keys and their values,
enumerating over the keys,
and saving the properties to a stream.
You can load properties files with
Properties applicationProps = new Properties();
try (FileInputStream in = new FileInputStream("appProperties")) {
applicationProps.load(in);
}
and store them with
try (FileOutputStream out = new FileOutputStream("appProperties")) {
applicationProps.store(out, "---No Comment---");
}

How to read all properties into an array with spring?

I know you can use spring to read a single property, and to read a single property that has a list of values into a list. But what about reading all the properties from a file into a list?
I.E.
EDIT: The property file we are reading is litterally just a list of values, no key, like the updated example below:
Property File
queueName1
quename2
queName3
...etc (the file is like 100 lines long hence why its not a list of values with one property name)
and then be able to do something like
//Imaginary Code
#Value("${GET ALL THE LINES}")
List<String> eachLineOfPropertyFile;
If you would want to do it using Spring alone, then separate each of the value using "," in the property file and make use of spring EL.
Your properties file will be like:
property.values=queueName1,quename2,queName3
And with Spring Value Annotation
#Value("#{'${property.values}'.split(',')}")
List<String> eachLineOfPropertyFile;
Can you not use the following
List<String> list = Files.readAllLines(new File("propertiesFile").toPath(), Charset.defaultCharset() );
PS: This is part of Java 7

Reading Java property groups from a file

Is it possible to read different property groups from a Java file, without manual processing?
By "manual" I mean reading the file line by line, detecting where the start of a property group is and then extracting the corresponding key-value pairs. In practice, this means reinventing (most of) the wheel that the Properties.load() method constitutes.
Essentially, what I am looking for is an easy way of reading, from a single file, multiple groups of properties, with each group being identifiable, so that it can be loaded in its own Java Properties object.
I you want to use java.util.Properties you can use prefixes. In .properties file:
group1.key1=valgroup1key1
group2.key1=valgroup2key1
group2.key2=valgroup2key2
and read them like this:
class PrefixedProperty extends Properties {
public String getProperty(String group, String key) {
return getProperty(group + '.' + key);
}
}
and using:
/* loading, initialization like for java.util.Properties */
String val = prefixedProperty.getProperty("group1", "key1");
You can also use ini4j with windows ini files.
Another, better way is using own, custom structured file (for example XML).

Categories

Resources