In enterprise application development we need to manage various configuration depends on your environment or profile.
i.e. we need to provide profile specified configuration to deployable .
Profile specific means we need to change our configuration for
- Local Development Environment
- QA Test Environment
- Staging Environment
- Production Environment
Or this classification may vary for different organization.
Spring Boot provides facility to provide multiple configuration using application.properties
and we can switch it as per our environment with minimum change.
Naming Convention
Out typical boot application creates default application.properties
which resides in resources directory, it will be loaded by default when application starts.
Apart from that you can create multiple property files for your environment.
Lets say i need to provide configuration for my local environment and production environment, so in that case i need to create 2 more properties files.
Naming pattern for new properties files will be application-{profile}.properties
, you can replace {profile}
with your actual profile/environment name.
Here i need to create
- application-local.properties
- application-production.properties
for example my datasource will be different in local and production environment, so my files will looks like below.
application-production.properties
spring.datasource.url=jdbc:mysql://localhost:3306/kode12?createDatabaseIfNotExist=true spring.datasource.username=root spring.datasource.password=<MY_PRODUCTION_PASSWORD> spring.datasource.driverClassName=com.mysql.jdbc.Driver
application-local.properties
spring.datasource.url=jdbc:mysql://localhost:3306/kode12?createDatabaseIfNotExist=true spring.datasource.username=root spring.datasource.password=<MY_LOCAL_PASSWORD> spring.datasource.driverClassName=com.mysql.jdbc.Driver
Here i created 2 different properties files with different configuration for different profile.
Profile Activation
Now let’s see how to set particular profile as an active profile.
To setup active profile you need to add below line in your application.properties
file.
spring.profiles.active=production
This line will use configuration from file application-production.properties
as we provided production
as default profile in application.properties
file using spring.profiles.active=production
.
Share current post by copy: http://goo.gl/IyM69W
Thanks,
Yogesh P
The post Spring Boot: Environment friendly application properties appeared first on kode12.