Confige place holder in properties file with Java Code - Spring Boot - spring-boot

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.

Related

How to process property file values during server start up in Spring?

I have this entry in application.properties of my spring boot app:
myapp.urls = url1,url2,url3
In a method in my component, I am creating an array like below:
String myArray[] = properties.getmyAppUrls().split(",");
I want this array creation logic execute only once. I know we can achieve this using post construct. Is there any other way we could achieve this like during server start up?
I want this array constructed reading from a properties file during server start up and i want to use this in my component.
You can use Spring EL to do the job:
#Value("#{'${myapp.urls}'.split(',')}")
private List<String> myAppUrls;
I'd recommend to move this to a Configuration class, then you can autowire it everywhere you need it.

Generate LocalDateTime in application.properties

I have Spring Boot application.
I need to generate LocalDateTime.now value in application properties.
I think there should be function like ${random.int} which generates random value

How to make properties file in Spring boot Primary and secondary

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.

Spring boot Load all files based on pattern and place it in HashMap

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?

Read properties file in original order from Spring Application Context

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.

Resources