background
You do not want to upload private information such as IDs and passwords of various cloud services to open source places such as GitHub.
.env
what is
.env
Files are generally used to store project environment variables. Generally, information such as cloud service passwords and database IPs can be .env
stored in files instead of being placed in clear text in the project's configuration file (for example application.yml
).
Usually, software or frameworks have methods to support .env
file loading configuration (instead of asking engineers to hand-write the program for loading environment variables during the project loading phase).
.env
The following describes how to save, load and use private information through the Spring Boot framework and files.
process
Step 1: .gitignore
Add to the file.env
.env
Private information can generally be stored in files. If we do not want to upload this private information to open source repositories such as GitHub, we must configure .env
the file accordingly, that is, configure it to be ignored by git:
.vscode/
.idea/
...
.env
Step 2: Create .env
the file
.env
The file stores data in KV format. The file format is the same *.properties
. It is recommended to capitalize the Key:
USERNAME=username
PASSWORD=password
Step 3: application.yaml
Configure and use in Spring Boot
application.yml
Just configure it in spring.config.import
:
spring:
config:
import: optional:file:.env[.properties]
app:
username: ${
USERNAME}
password: ${
PASSWORD}