Multiple external property file in spring boot from command line
This is how u can load properties from any location using environment variable
-Dspring.config.location="C:\Project\properties\", -Dsecure.properties.location="C:\Project\properties\security\dev\"
@PropertySources({
@PropertySource("file:${spring.config.location}/configuration.properties"),
@PropertySource("file:${secure.properties.location}/secure.properties")})
How does spring handle multiple property-files?
I may be late here, but ran into similar issue. The interesting point is springboot doesn't handle the yml file same way as it does .properties. For @PropertySource to load the yml correctly you need to provide the factory class.
Also, you are right about not using the application.yml in the library project as it will conflict with the later application referencing this library project.
Few good references,
Spring reference - https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-exposing-yaml-to-spring
For implementation reference - https://www.baeldung.com/spring-yaml-propertysource
What is the point of creating external configuration file in spring boot?
The point of application.properties is to be able to change some properties without re-compiling your code. So in big companies no QA phase, delivery needed
You just change some properties, restart the application and ready to go.
Externalizing configuration in Spring Boot with multiple applications running in the same container
The suggestions of @Iulian Rosca to use a pattern like ${properties_home}/${application_id}/application.properties
brought me to the idea of defining a custom JVM property like app.config.root
and using this property to override spring.config.location
very early in the application lifecycle.
My application class looks now like this and works for embedded and container deployments:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return configureApplication(builder);
}
public static void main(String[] args) {
configureApplication(new SpringApplicationBuilder()).run(args);
}
private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
return builder
.sources(Application.class)
.properties("spring.config.location:${${app.config.root}/myapp1/:#{null}}");
}
}
Important notes to this solution:
app.config.root
has to be set externally by JVM or JNDI propertyapp.config.root
can only contain a single external configuration path (for my requirements this was sufficient) in contrast tospring.config.location
where multiple comma-separated paths can be specifiedSpringApplicationBuilder.properties(...)
sets the application's default properties. Because of this,spring.config.location
cannot be specified externally anymore, as JVM or JNDI Properties have priority over default properties and would therefore overridespring.config.location
again.
Related Topics
Do I Need <Class> Elements in Persistence.Xml
How to Make a Jar File That Includes Dll Files
Java.Lang.Unsupportedclassversionerror: Bad Version Number in .Class File
Random Weighted Selection in Java
Java - Best Approach to Parse Huge (Extra Large) JSON File
Nosuchmethoderror in Javax.Persistence.Table.Indexes()[Ljavax/Persistence/Index
Why Does a Java Method Reference with Return Type Match the Consumer Interface
Sqlexception: No Suitable Driver Found for Jdbc:Derby://Localhost:1527
What Causes "'Void' Type Not Allowed Here" Error
The JPA Hashcode()/Equals() Dilemma
What Does the Java Assert Keyword Do, and When Should It Be Used
Why Do People Still Use Primitive Types in Java
How to Specify Jackson to Only Use Fields - Preferably Globally
Why Does Java Prohibit Static Fields in Inner Classes