


Utilizing Environment Variables in Spring Boot's Application.properties
When deploying a Spring Boot application across various environments, it's essential to avoid hard-coding database credentials in application.properties. To address this, environment variables can be dynamically referenced in the property file.
To leverage this approach, follow these steps:
1. Create System Environment Variables
Create environment variables locally and on any other relevant virtual machines. Name these variables identically to their OpenShift counterparts and assign appropriate values:
export OPENSHIFT_MYSQL_DB_HOST="jdbc:mysql://localhost" export OPENSHIFT_MYSQL_DB_PORT="3306" export OPENSHIFT_MYSQL_DB_USERNAME="root" export OPENSHIFT_MYSQL_DB_PASSWORD="123asd"
2. Referencing Environment Variables in application.properties
To include environment variables in application.properties, use the following syntax:
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}
3. Alternative Approach (Recommended)
However, a more concise and manageable solution proposed by @Stefan Isele is to use Spring profiles:
- Create multiple property files named application-{profile-name}.properties.
- Set the environment variable spring.profiles.active to the desired profile (e.g., application-local.properties).
Spring will automatically read the appropriate property file based on the specified profile.
By following these techniques, you can dynamically configure your Spring Boot application based on the environment in which it's deployed, without having to hard-code sensitive information in application.properties.
The above is the detailed content of How Can I Manage Spring Boot Database Credentials Across Different Environments Using Environment Variables?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.