Usually our program has multiple environments:
1. Development environment;
2. Production environment.
Wait
The configuration of the environment is different, we hope to switch the environment through a simple configuration, and springboot easily achieves this function:
1. Multiple environments require multiple configuration files
Generally, we have: application.properties configuration file by default , now we add two environment files , the file name format is: application-{profile}.properties :
1) application-dev.properties; // for development environment
2) application-prod.properties. // for production
The following configuration data source is an example:
The contents of application-dev.properties are as follows:
# development environment spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://本地IP:3306/test spring.datasource.username=root spring.datasource.password =Password
The contents of application-prod.properties are as follows:
# Production Environment spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://服务器IP:3306/test spring.datasource.username=root spring.datasource.password =Password
After configuring the file, we need to activate one of the configuration files and add the following configuration in application.properties:
# Environment configuration
spring.profiles.active=prod
As above, we activated the configuration file of the prod production environment, then when the program starts, it will naturally load the content of the application-prod.properties file