I need to read original order properties file from Spring Application context.
example:
A.properties
1:abc
2:xyz
3:qwe
The above file I need to get in the same order.
Related
Hi I am using Spring 4.3.7, java 8 and spring boot, My requirement is I have 2 properties file one inside the classpath and another outside. I was able to load both. using
#PropertySources({ #PropertySource("classpath:common.properties"), #PropertySource("classpath:anotherFile.properties") })
#PropertySource(value = {"file:${external.config.location}/config_one.properties"}, ignoreResourceNotFound = true)
the input values of both the file will be almost same eg file naming convention or file create location (besides db details and few other token details)
what has to be done is if external property file exist read the property value from it or else read from one inside the classpath. is this possible via any annotation in Spring boot?
It works out of the box. The difference is that it does not "alternatively" read prop from one or another source, but it rather reads all properties from the first source, then reads all properties from another (and overrides eventual duplicates), than moves to the third source... and so on and on
There are in total 17 "default" sources and all have its own precedence over the others. See more in docs
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
Please bare in mind, that those sources are read "from bottom to top", so eg key from internal application.properties #15 will be overriden by key from external appication.properties #14 and so on.
I have the following case and I am trying to solve it with Spring-Boot and Spring-Batch. I have to read a flat file (sidecar file) in which every line is a name of a file to be ingested in a database.
I have configured a job to read the sidecar file, but I am having a problem to decide what is the accepted method in spring-batch to process the contained files. I also have configured steps that can read each file and insert the records in a data base.
Any ideas how to configure the sidecar job with the steps I have written for the individual files.
I can provide actual configuration from my implementation if needed.
FactoryBean is your friend. In this case, you can create a FactoryBean that reads the file of file names and creates a list of Resource objects for it. That can be injected into the MultiResourceItemReader which will iterate over that list.
I am developing an app where there will be a properties file for each type of database.
I want to load the properties file corresponding to the database based on the driver available in classpath using spring boot custom auto configuration and put it in a common holder object as a map.
For example, lets say I have oracle driver on classpath, So i need to load oracle-metadata.properties and place it in map with key as "oracle" and value as the properties object.
Current Approach
#ConfigurationProperties
#PropertySource("classpath:oracle-metadata.properties")
public class OracleMetadataConfiguration{
//if i have the object attributes corresponding to properties file, i will end up having duplicate attributes for each type of database
}
Application.properties
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.**.autoconfiguration.OracleMetadataConfiguration
How do i use the different instance representation for same configuration object structure?
What is the best way to design this?
I find Spring Boot's (or spring in general) handling of yaml collections to be a bit peculiar. Collections according to yaml specs should be written in .yaml files as:
myCollection: ['foo', 'bar']
or
myCollection:
- foo
- bar
But neither #Value("${myCollection}") annotation or Environment.getProperty("myCollection", String[].class) (also tried List.class) can read collection properties (returns null). The only method I know of that works is to use #ConfigurationProperties annotation described in spring boot docs.
The problem with #ConfigurationProperties annotation is that (a) it is too verbose if all I want is a single property and (b) it rely on bean injection to get an instance of the #ConfigurationProperties class. Under some circumstances, bean injection is not available and all we have is a reference to Environment (e.g: thru ApplicationContext).
In my particular case, I want to read some properties during ApplicationEnvironmentPreparedEvent event, since it happens before context is built, the listener has to be manually registered and therefore, no bean injection. Via the event argument, I can get a reference to Environment. So, I can read other properties but cannot read collections.
A couple of "solutions" I noted (quoted because I don't find them very satisfactory):
Specify collections in .yaml file as myCollection: foo, bar. But this is not ideal because, the format isn't really yaml anymore.
Read individual elements using an index, for example Environment.getProperty("myCollection[0]", String.class). Will require some not-so-elegant utility methods to read and put all elements into a List.
So, my questions is - What is a good way to read collection-type properties if I cannot use #ConfigurationProperties? Also curious why comma-separated format works but not yaml-style collections.
EDIT: corrected some typos
Quite Frankly Spring boot application.properties and application.yaml or application.yml is meant to load configuration properties.
The #ConfigurationProperties annotation is designed as an abstraction to hide the implementations of configuration properties and support both .properties and .yaml/.yml.
For yaml/yml however Spring uses org.yaml.snakeyaml.Yaml library underneath to parse the file and load it to a Properties object inside org.springframework.boot.env.YamlPropertySourceLoader and a Collection is mapped as a Set not an array or List. So you try doing the following;
Environment.getProperty("myCollection", Set.class)
I have properties file in Spring Boot application , with end points mentioned as below :-
user.details = /api/{userID}/get-user
I am using POJO with #PropertySource, #Configuration to read values.
Now my requirement is to replace the userID value dynamically from the java code after reading it from properties file, when I receive the ID from front end application. I will not pass this value from command line as it does not serve my use case.