springboot: config in "additional-location" was overrided by "application-{profile}"

Viewed 179

It is a simple springboot project. only has a few properties and a MainClass:

@Slf4j
@SpringBootApplication
public class DemoApplication {

    @Value("${test}")
    String test;

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

    @PostConstruct
    public void print() {
        log.info(test);
    }
}

It just print the value of variable test.

I configure test in application.properties:

test=test normal

spring.profiles.active=dev

The variable was overrided by application-dev.properties:

test=test in dev

Then I run the application, It works. It print: test in dev

The next thing is problem:

I package the application as a jar, and I want to override the test when I run this application, So I write a file out.properties:

test=test in out

Start the application by command

java -jar target/demo.jar --spring.config.additional-location=out.properties

It still prints test in dev!

I change the command to:

java -jar target/demo.jar --test="test in command"

It print test in command.

I have read the document: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config. But I didn't find some useful tip.

My purpose is replace some property in application with a out config, so I don't have to change the source code and repackage.

Thanks for help!

1 Answers

The linked documentation specifically lists the priority order of property sources:

1-3. ...

  1. Command line arguments.

5-11. ...

  1. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).

  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).

  3. Application properties outside of your packaged jar (application.properties and YAML variants).

  4. Application properties packaged inside your jar (application.properties and YAML variants).

16-17. ...

As your code already showed you, (4) --test="test in command" overrides anything in (12-15) property files.

You can also see that (12-13) profile-specific application property files always override (14-15) non-profile application property files.

So, if you want an external file to override a (13) packaged profile-specific application property file, then you must place that property in a (12) external profile-specific application property file.

But, as the note in section 2.4. Profile-specific Properties says:

If you have specified any files in spring.config.location, profile-specific variants of those files are not considered. Use directories in spring.config.location if you want to also use profile-specific properties.

Said another way, any file listed in spring.config.location is by definition a (14) application property file, regardless of how it is named, so (12-13) profile-specific application property files will override them, whether (13) packaged or (12) external.

Summary: Move the test=test in out property to an application-dev.properties file in a directory listed in the --spring.config.additional-location path.

Related