简体   繁体   中英

Accessing environment variables from .env file in springboot on AWS EC2

I want to set my environment variables in ".env" file for spring boot application that's running on EC2 instance, and access them in code. I know i can set environment variables on EC2 like so

export env="dev"
export db.url="xyz"

But i dont want to do this, instead i want to maintain these environment variables in a ".env" file and access them in spring boot application like

@Value("${env}")
public String env;

I'm able to use export and read the env variables but can't get around getting the variables from .env file. I don't want to use shell script either for loading the variables explicitly. Is there a way i can achieve this?

Have you managed to do it locally before deploying to an EC2 instance? Did you try to inject the properties using ".properties" file? It seems like the answer you are looking for. spring-boot-application-configuration

If you manage to do it locally, there is not reason you wouldn't be able to do it in an EC2 instance.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM