user218046
user218046

Reputation: 633

How to override Spring Boot library properties?

I would like to override the library properties that I've written in project that is using it. I'm using this Spring guide on creating library: https://spring.io/guides/gs/multi-module/

I would like to know how to override for example my.properties file in the project that uses my library.

Is it even possible?

Upvotes: 0

Views: 4711

Answers (4)

Lakatos Gyula
Lakatos Gyula

Reputation: 4160

Do not add application.properties files to a library. It could cause a myriad of problems. If you want to set a default property do it like this:

@ConfigurationProperties("foo")
public class FooConfig {

    private int bar = 999;

    // getter / setter

}

Every application should configure the library values for itself in its own application.properties file.

Upvotes: 2

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24618

Depends on how the library property file specifies properties. If the properties are looked up from environment/system first, then you can override in your code. If hard-coded, then not. For example:

prop=${ENV_VAR:abc}

You can set ENV_VAR environment variable, or env-var system variable in your code to override the value of prop. If you don’t, the default value abc will be used.

Upvotes: 0

Rob Scully
Rob Scully

Reputation: 790

Spring Boot property order precedence is described here:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

From the link:

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

The order is 1-17 then. For example, 4 on the list (command-line arguments) overrides 15 on the list (your application.properties file) and so on.

Upvotes: 0

Qingfei Yuan
Qingfei Yuan

Reputation: 1212

Seems you cannot. Since Spring itself also cannot

In the sample above we have configured the service.message for the test using the default attribute of the @SpringBootTest annotation. It is not advisable to put application.properties in a library because there might be a clash at runtime in the application that uses it (only one application.properties is ever loaded from the classpath). You could put application.properties in the test classpath, but not include it in the jar, for instance by placing it in src/test/resources.

Upvotes: 0

Related Questions