I am trying to build a Springboot application which connects to a DB. I would like to use a .env
file which has the sensitive content. At the first hand, I am testing by changing the port to 8081. My .env file has the following content
PORT=8081
My application.properties has the following content
server.port=${PORT}
I have a run time error that PORT cannot be resolved, which is to be expected when I did not know how to feed the .env file to properties.
Could someone point me in the right direction?
PS: I am using the port as an example, if this succeeds I will also set the DB Credentials with the .env file.
UPDATE: I would prefer using .env
file because when the application is deployed using AWS CodePipeline, I can have the environment variables set in the CodeBuild stage where I would be building the jar and eventually a docker image in this stage. Something like this.
EnvironmentVariables:
- Name: PORT
Value: "{resolve:secretsmanager:DBCredentials:SecretString:port}"
The error is Caused by java.lang.IllegalArgumentException: Could not resolve placeholder 'PORT' in value "${PORT}"
I think you have the react approach ! But in spring for using multiple environments, it's better to have a properties file or yaml by environment.
Ps: File name must be named like application-{environment name}.properties
and must be in the resources folder.
For dev: File name : application-dev.properties
server.port=8089
For IT: File name : application-it.properties
server.port=8090
In the application.properties
file, where usually we put some shared properties between all the environments, we can add the propertie : spring.profiles.active = dev
// you can put what you want depending on your needs. If you have more than one profile you can separate them by " ,
".
For more details you can check spring profiles
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.