Neo
Neo

Reputation: 2226

How to add properties programmatically(just like adding key-value into application.properties)?

I have some common properties that every projects should set, such as

feign.hystrix.enabled=false
feign.httpclient.enabled=true

I don't want to repeatedly add these props in every project so I'm going to create an extra jar file containing @Configruation class. How to add properties in @Configuration class? Thanks!

Upvotes: 1

Views: 3843

Answers (1)

alexbt
alexbt

Reputation: 17025

PropertySources

You may load an application.properties from another jar this way:

@PropertySources({
    @PropertySource("classpath:common.properties")
})
@Configuration
public class SomeJavaConfig {
}

You can find the reference in Spring's documentation:

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

...

  1. @PropertySource annotations on your @Configuration classes.

Spring-cloud-config

I won't go in all the details, but another option is to use spring-cloud-config to define these properties in a git (using spring-cloud-config-server). Then, have your spring-boot application load the application.properties using spring-cloud-config-client directly from git.

Check this:

Upvotes: 2

Related Questions