简体   繁体   English

在AWS EC2上的springboot中从.env文件访问环境变量

[英]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. 我想在EC2实例上运行的Spring Boot应用程序的“ .env”文件中设置环境变量,并通过代码访问它们。 I know i can set environment variables on EC2 like so 我知道我可以像这样在EC2上设置环境变量

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 但是我不想这样做,相反,我想在“ .env”文件中维护这些环境变量,并在spring boot应用程序中访问它们,例如

@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. 我可以使用export并读取env变量,但是无法从.env文件中获取变量。 I don't want to use shell script either for loading the variables explicitly. 我也不想使用shell脚本来显式加载变量。 Is there a way i can achieve this? 有没有办法可以做到这一点?

Have you managed to do it locally before deploying to an EC2 instance? 在部署到EC2实例之前,您是否已在本地完成此操作? Did you try to inject the properties using ".properties" file? 您是否尝试使用“ .properties”文件注入属性? 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. 如果您设法在本地执行此操作,则没有理由在EC2实例中无法执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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