Using Environment Variables in Spring Boot’s application.properties – 在Spring Boot’的application.properties中使用环境变量

最后修改: 2022年 8月 2日

中文/混合/英文(键盘快捷键:t)

 1. Overview

1.概述

In this tutorial, we’ll explain how to use environment variables in Spring Boot’s application.properties. Then we’ll demonstrate how to refer to those properties in the code.

在本教程中,我们将解释如何在Spring Boot的application.properties中使用环境变量。然后我们将演示如何在代码中引用这些属性。

2. Use Environment Variables in the application.properties File

2.在application.properties文件中使用环境变量

Let’s define a global environment variable called JAVA_HOME with the value “C:\Program Files\Java\jdk-11.0.14”.

让我们定义一个全局环境变量,名为JAVA_HOME,其值为 “C:\Program Files\Java\jdk-11.0.14″。

To use this variable in Spring Boot’s application.properties, we need to surround it with braces:

要在Spring Boot的application.properties中使用这个变量,我们需要用大括号将其包围。

java.home=${JAVA_HOME}

We can also use the System properties in the same way. For instance, on Windows, an OS property is defined by default:

我们也可以以同样的方式使用系统属性。例如,在Windows上,默认定义了一个操作系统属性。

environment.name=${OS}

It’s also possible to combine several variable values. Let’s define another environment variable, HELLO_BAELDUNG, with the value “Hello Baeldung”. We can now concatenate our two variables:

也可以把几个变量值结合起来。让我们定义另一个环境变量,HELLO_BAELDUNG,其值为 “Hello Baeldung”。现在我们可以把我们的两个变量串联起来。

baeldung.presentation=${HELLO_BAELDUNG}. Java is installed in the folder: ${JAVA_HOME}

The property baeldung.presentation now contains the following text: “Hello Baeldung. Java is installed in the folder: C:\Program Files\Java\jdk-11.0.14”.

property baeldung.presentation现在包含以下文字。”你好Baeldung。Java被安装在文件夹中。C:\Program Files\Java\jdk-11.0.14″。

This way, our properties have different values depending on the environment.

这样一来,我们的属性就会根据环境的不同而有不同的价值。

3. Use Our Environment Specific Properties in Code

3.在代码中使用我们的环境特定属性

Given that we start a Spring context, we’ll now explain how we can inject the property value into our code.

鉴于我们启动了一个Spring上下文,我们现在将解释如何将属性值注入我们的代码中。

3.1. Inject the Value With @Value

3.1.用@Value注入值

First, we can use the @Value annotation. @Value handles setter, constructor, and field injections:

首先,我们可以使用@Value注释。@Value处理setter、constructor和field injections

@Value("${baeldung.presentation}")
private String baeldungPresentation;

3.2. Get It From Spring Environment

3.2.从Spring环境中获得它

We can also get the value of the property via Spring’s Environment. We’ll need to autowire it:

我们还可以通过Spring的Environment获得该属性的值。我们需要autowire它。

@Autowired
private Environment environment;

The property value can now be retrieved thanks to the getProperty method:

现在可以通过getProperty方法来检索属性值。

environment.getProperty("baeldung.presentation")

3.3. Group Properties With @ConfigurationProperties

3.3.用@ConfigurationProperties分组属性

The @ConfigurationProperties annotation is very useful if we want to group properties together. We’ll define a Component that will gather all properties with a given prefix, in our case baeldung. Then we can define a setter for each property. The name of the setter is the rest of the name of the property. In our case, we have only one called presentation:

如果我们想把属性分组,@ConfigurationProperties注解就非常有用。我们将定义一个Component,它将收集所有具有特定前缀的属性,在我们的例子中是baeldung。然后我们可以为每个属性定义一个setter。设置器的名称就是属性的其余名称。在我们的例子中,我们只有一个叫做presentation的。

@Component
@ConfigurationProperties(prefix = "baeldung")
public class BaeldungProperties {

    private String presentation;

    public String getPresentation() {
        return presentation;
    }

    public void setPresentation(String presentation) {
        this.presentation = presentation;
    }
}

We can now autowire a BaeldungProperties object:

我们现在可以自动连接一个BaeldungProperties对象。

@Autowired
private BaeldungProperties baeldungProperties;

Finally, to get the value of a specific property, we need to use the corresponding getter:

最后,为了获得一个特定属性的值,我们需要使用相应的getter。

baeldungProperties.getPresentation()

4. Conclusion

4.总结

In this article, we learned how to define properties with different values depending on the environment, and use them in the code.

在这篇文章中,我们学习了如何根据环境定义不同值的属性,并在代码中使用它们。

As always, the code is available over on GitHub.

像往常一样,代码可在GitHub上获得