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
Related
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 ?
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.
I have the below class which is using google guava map as you can see below , now the only query is that in it a list is been created as you can see but i want to keep this list in a properties file seprately since later on many new items will be added and deleted in this list so i want developer to only change the properties file
Below is the code
final Table<String, String, List<String>> values = HashBasedTable.create();
values.put("Tbon", "cy", Lists.newArrayList("cat1","cat12","cat13","cat14"));
values.put("Tbon", "ype", Lists.newArrayList("rat1","rat2","rat3"));
for (Cell<String, String, List<String>> cell: values.cellSet()){
System.out.println(cell.getRowKey()+" "+cell.getColumnKey()+" "+cell.getValue());
}
now as you can see both the array list are hardcoded here "cat1","cat12","cat13","cat14" and "rat1","rat2","rat3" but i want to keep thsese array list in a properties file seprately so that it can be map against row key so the properties file content will be like lets say name of the properties file is abc.properties stored in my local computer C: drive
cy cat1
cy cat12
cy cat13
cy cat14
ype rat1
ype rat2
ype rat3
so please advise if i keep this mappings in a properties filethen how they will be get called in the data structure values
There is plenty of information on how to write property files on your disk on the internet, e.g. here: http://www.mkyong.com/java/java-properties-file-examples/
The default format assumes one value per key so you'll need some splitting and joining. Here's a simple answer to that issue: Multiple values in java.util.Properties
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?
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).