0

I would like override SpringBoot external's some config into classpath file., when I run jar by command line.

classpath -> application-config.yaml (Not application.yaml)

server:
  port: 8080
  servlet:
    contextPath: /myapp
test-message: this config no need to export as external config.

external -> D:/test/application-config-override.yaml

server:
  port: 9090

command

java -Dspring.config.location=classpath:application-config.yaml,file:///D:/test/application-config-override.yaml -jar myapp.jar

when I run above commend, the application is running 8080, contextPath is "myapp". My expected one is http://localhost:9090/myapp

But, if I put all of same config structure of application-config.yaml into application-config-override.yaml as below

application-config-override.yaml

server:
  port: 9090
  servlet:
    contextPath: /yourapp
test-message: this config no need to export as external

Application is running 9090, contextPath is "yourapp".

How to override SpringBoot external some config into classpath file? I am using 2.5.4.

I don't want to export unnecessary some configs to external. I would like to marge and override by order.

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131

3 Answers3

1

Try to provide both spring.config.additional-location and spring.config.name like this,

-Dspring.config.additional-location=D:///test/ -Dspring.config.name=application-config,application-config-override
ray
  • 1,512
  • 4
  • 11
1

In Windows,

java -jar demo.jar --spring.config.additional-location=file:///C:/Data/demo/application-config-override.yaml --spring.config.name=application-config,application-config-override

In linux,

java -jar demo.jar --spring.config.additional-location=file:/home/user/config/application-config-override.yaml --spring.config.location=classpath:application-config.yaml

The above command would solve your need. Tested on Spring version 2.5.4

enter image description here

Kyaw Bo
  • 111
  • 1
  • 11
0

Since you are using SpringBoot why are you not using the SpringBoot naming conventions? If you name the files application.yaml then you will not have to run the jar specifying the locations of your config files as SpringBoot will automatically load them in (if they are in a default location).

Instead of application-config.yaml and application-config-override.yaml name both the internal and external files application.yaml.

Next, if you place the yaml file outside the jar it will automatically get read in by SpringBoot and do what you are trying to achieve.

Here is a test I ran:

internal application.yaml file:

value1: "internal" 
value2: "internal"

then in the application:

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

@Value("${value1}")
String val1;

@Value("${value2}")
String val2;


public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
    System.out.println("Value1: " + val1 + "\n" + "Value2: " + val2);
}

}

Then when I run this with no external config:

java -jar demo-0.0.1-SNAPSHOT.jar

INFO 6604 --- [ main] com.example.demo.DemoApplication
: Started DemoApplication in 1.611 seconds (JVM running for 2.118) Value1: internal Value2: internal

Then I place an external application.yaml in the same directory as the jar:

demo-0.0.1-SNAPSHOT.jar 
application.yaml

Having value:

value1: "external"

Then the output is:

java -jar demo-0.0.1-SNAPSHOT.jar

2021-09-05 11:22:31.211 INFO 23540 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.552 seconds (JVM running for 2.101) Value1: external Value2: internal

So you can see the external value1 overrides the internal value1

These configuration files are loading in a specific order and precedence. From SpringBoot Docs:

Config data files are considered in the following order:

  • Application properties packaged inside your jar
  • Profile-specific application properties packaged inside your jar
  • Application properties outside of your packaged jar
  • Profile-specific application properties outside of your packaged jar
Fermi-4
  • 645
  • 6
  • 10
  • Thanks for your answer. It is worked at 2.1.x verison. It is hard to change to for all project. We have over one hundred microservices. – Zaw Than oo Sep 05 '21 at 17:28