Java 在 SpringBoot 中读取环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44803211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read environment variable in SpringBoot
提问by Micha? Szewczyk
What is the best way to read environment variablesin SpringBoot?
In Java I did it using:
在SpringBoot 中读取环境变量的最佳方法是什么?
在Java中,我使用:
String foo = System.getenv("bar");
Is it possible to do it using @Value
annotation?
是否可以使用@Value
注释来做到这一点?
采纳答案by g00glen00b
Quoting the documentation:
引用文档:
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variablesand command-line arguments to externalize configuration. Property values can be injected directly into your beans using the
@Value
annotation, accessed via Spring'sEnvironment
abstraction or bound to structured objects via@ConfigurationProperties
.
Spring Boot 允许您将配置外部化,以便您可以在不同环境中使用相同的应用程序代码。您可以使用属性文件、YAML 文件、环境变量和命令行参数来外部化配置。属性值可以使用
@Value
annotation直接注入到您的 bean 中,通过 Spring 的Environment
抽象访问或通过 .bin绑定到结构化对象@ConfigurationProperties
。
So, since Spring boot allows you to use environment variables for configuration, and since Spring boot also allows you to use @Value
to read a property from the configuration, the answer is yes.
因此,由于 Spring boot 允许您使用环境变量进行配置,并且 Spring boot 还允许您使用@Value
从配置中读取属性,因此答案是肯定的。
This can be tested easily, the following will give the same result:
这可以很容易地测试,以下将给出相同的结果:
@Component
public class TestRunner implements CommandLineRunner {
@Value("${bar}")
private String bar;
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void run(String... strings) throws Exception {
logger.info("Foo from @Value: {}", bar);
logger.info("Foo from System.getenv(): {}", System.getenv("bar")); // Same output as line above
}
}
回答by Rlarroque
You can do it with the @Value annotation:
您可以使用 @Value 注释来做到这一点:
@Value("${bar}")
private String myVariable;
You can also use colon to give a default value if not found:
如果未找到,您还可以使用冒号给出默认值:
@Value("${bar:default_value}")
private String myVariable;
回答by nobar
Here are three "placeholder" syntaxes that work for accessing a system environment variable named MY_SECRET
:
以下是用于访问名为 的系统环境变量的三种“占位符”语法MY_SECRET
:
@Value("${MY_SECRET:aDefaultValue}")
private String s1;
@Value("#{environment.MY_SECRET}")
private String s2;
@Value("${myApp.mySecretIndirect:aDefaultValue}") // via application property
private String s3;
In the third case, the placeholder references an application property that has been initialized from the system environment in a properties file:
在第三种情况下,占位符引用了一个应用程序属性,该属性已从属性文件中的系统环境初始化:
myApp.mySecretIndirect=${MY_SECRET:aDefaultValue}
For @Value
to work, it must be used inside a live @Component
(or similar). There are extra gochas if you want this to work during unit testing -- see my answer to Why is my Spring @Autowired field null?
为了@Value
工作,它必须在 live @Component
(或类似的)中使用。如果您希望它在单元测试期间工作,还有额外的 gochas - 请参阅我对为什么我的 Spring @Autowired 字段为空的回答?
回答by Saikat
Alternatively, you can use the org.springframework.core.env.Environment
interface to access environment variables:
或者,您可以使用该org.springframework.core.env.Environment
接口访问环境变量:
@Autowired
private Environment env;
...
System.out.println(env.getProperty("bar"));