Spring Property File
Property file can be either .yml
or .property
. Refer to Spring External Configuration
[!important]
When populating these files, always use kebab case (this-is-my-property
). It's because if we do${application.this-is-my-property}
it will match with bothapplication.thisIsMyProperty
andapplication.THIS_IS_MY_PROPERTY
forms.If you use
application.thisIsMyProperty
then it will not match withthis-is-my-property
orTHIS_IS_MY_PROPERTY
Property placeholder
You can use spring property placeholder to refer to other properties for example:
spring:
main:
lazy-initialization: off
application:
name: "Springboot learning application (${environment})"
name: "Austin"
environment: "nonprod"
For example in here the ${environment}
will refer to the value nonprod
Multi-document
A document can have multi-document within a single document. For example:
spring:
application:
name: "Springboot learning application (${environment})"
---
spring:
application:
name: "Springboot practice application"
In here we define two different set of document within 1 file. Separated by the ---
.
As a result, Spring will pick the later one to overwrite the previous one.
In this case, the spring.application.name
will be Springboot practice application
Normally, we want this section to be activated within a specific condition. In that case, we can use spring.config.activate.on-profile
spring:
application:
name: "Springboot learning application"
---
spring:
config:
activate:
on-profile: "prod"
application:
name: "Springboot practice application"
As a result, the spring.application.name
will only be overwrite to Springboot practice application
in prod environment