Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot: chain application properties from a starter?

I'm developing a spring boot app and starter. I want the starter to hold common properties and code.

In my starter I have an application-dev.properties file. I have defined a @Profile("dev") in the starter.

When I start my app using the dev profile I see properties from the starter, which is great. What's not great is when I add an application-dev.properties file to my app, the one from the starter is no longer read at all. This means I can't figure out how to put common "dev" profile properties in there.

Based on the external configuration documentation I assumed this would work because the starter file would be read and then the app file.

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

SNIP

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

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

Any suggestions?

like image 594
donahchoo Avatar asked Jan 31 '17 21:01

donahchoo


2 Answers

Putting application-dev.properties into src/main/resources/config folder for your starter and putting application-dev.properties of your app into src/main/resources should help resolve this problem.

like image 68
anand1st Avatar answered Oct 23 '22 04:10

anand1st


I think adding application-dev.properties to starter is not good idea. The problem is you can add the file only once. Well, you may put the file into folder config. It means you can add the file at most twice. No more. What if you decide to use second starter; where you put application-dev.properties then?

I think application*.properties should be added to end user product only. For portable module I propose to use

  1. @PropertySource annotations on your @Configuration classes.
@Configuration
@PropertySource("classpath:/my/starter/dev.properties")
@Profile("dev")
public class MyConf {}
like image 24
michaldo Avatar answered Oct 23 '22 05:10

michaldo