Consider a Spring Boot application connecting to MySQL in multiple environments (local, Jenkins, OpenShift). To avoid hardcoding MySQL credentials, you need to make application.properties dynamic. As a proposed solution, you've created system environment variables with the same names as OpenShift environment variables and assigned appropriate values.
To incorporate system environment variables into application.properties, add the following lines:
spring.datasource.url = ${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/"nameofDB" spring.datasource.username = ${OPENSHIFT_MYSQL_DB_USERNAME} spring.datasource.password = ${OPENSHIFT_MYSQL_DB_PASSWORD}
When Spring Boot initializes, it will retrieve these environment variables and substitute them into your configuration.
An alternative approach is to use Spring Boot profiles. Add the following to application.properties:
spring.profiles.active=local
Create a new property file named application-local.properties containing:
spring.datasource.url=jdbc:mysql://localhost spring.datasource.username=root spring.datasource.password=123asd
Spring Boot will automatically load the properties from application-{profile-name}.properties based on the value of spring.profiles.active.
The above is the detailed content of How Can I Use Environment Variables to Manage Database Credentials in a Spring Boot Application?. For more information, please follow other related articles on the PHP Chinese website!