As M. Deinum mentions in his comment, the simplest way to do this is to use profile specific configuration.
Spring Boot allows you to have one common configuration file (application.properties
) and then multiple other files, each specific to a profile (application-${profile}.properties
).
For instance:
application.properties
- Common configuration
application-dev.properties
- Configuration for dev profile
application-ci.properties
- Configuration for ci profiles
If your application runs with "ci" profile for instance, the default configuration file as well as the ci configuration file (which would contain the datasource configuration properties for ci profile) will be loaded.
To switch profiles you can use one of the following options:
- JVM property:
-Dspring.profiles.active=ci
- Command line switch:
--spring.profiles.active=dev
For unit tests you can use @ActiveProfiles("test")
annotation on your test classes to tell Spring that unit tests should be run with test profile.
Also if you don't want to store production database credentials along with your source code, you can specify external configuration file when you deploy your app in production:
- Using command line switch:
--spring.config.location=/srv/myapp/config.properties
- Using a JVM property:
-Dspring.config.location=/srv/myapp/config.properties
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…