深圳幻海软件技术有限公司 欢迎您!

教你如何在 Spring BOOT 中处理配置

2023-02-28

大约8年后,我目前正在重新使用Java+Spring进行编码。在过去的8年中,我花在编码上的时间显着增加,因为我现在担任的领导角色让我远离了编写代码。话虽如此,我需要了解一定程度的编码,尤其是在Java世界中,因为这是我的大部分项目都使用的语言,除非我熟悉编码,否则我无法有效地帮助我的团队。自从我停

大约 8 年后,我目前正在重新使用 Java + Spring 进行编码。在过去的 8 年中,我花在编码上的时间显着增加,因为我现在担任的领导角色让我远离了编写代码。话虽如此,我需要了解一定程度的编码,尤其是在 Java 世界中,因为这是我的大部分项目都使用的语言,除非我熟悉编码,否则我无法有效地帮助我的团队。自从我停止编码以来发生了很大的变化,我正在重新学习一切。这是我将写的许多文章中的第一篇,因为我知道了新事物。此外,我更多地从我个人的成果中构建了一个应用程序。我通常不能花一致的时间,这让我可以花更多的时间学习,而不是试图满足现实生活中客户项目的最后期限。

在这篇文章中,我将讨论如何在 Spring Boot 应用程序中使用外部化配置。

1.概述

我们将使用 Spring Boot 的默认设置来创建一些配置并在我们的应用程序中读取它们。我们还将研究一种设置属性和基于 YAML 的配置的简单键值方式。

我更喜欢使用 YAML,从现在开始,我将只使用 YAML 基础设置。

2. 初始设置

在我的实现运行期间,我注意到我最终需要使用。

spring-boot-configuration-processor 依赖项。否则我会得到一个错误,并且代码不会编译。我没有找到太多关于为什么在我的研究中需要这样做,但是添加这个依赖项为我解决了这个问题。

我添加的第二个依赖项是 Actuator,它为我们提供了一些令人兴奋的工具。这是不必要的,但如果您正在寻找调试属性并找到更多可使用的配置,我建议您也添加它。

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"><依赖></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            <groupId>org.springframework.boot</groupId></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            <artifactId>spring-boot-configuration-processor</artifactId></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            <可选></可选></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        </依赖></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        <依赖></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            <groupId>org.springframework.boot</groupId></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            <artifactId>spring-boot-starter-actuator</artifactId></font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        </依赖></font></font>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

3. 设置配置文件

以下代码适用于 application.properties 文件,这是您将在 Spring Boot 中获得的默认格式。

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"># 这是为了暴露Actuator的所有端点。</font><font style="vertical-align: inherit;">仅在 DEV 中对所有人使用 *</font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
management.endpoints.web.exposure.include=*</font></font>
 
# Custom Properties
bungie.rootPath="https://www.bungie.net/Platform"
bungie.apiKey=000999888111
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

以下是 YAML 格式的相同属性。

bungie:
  rootPath: "https://www.bungie.net/Platform"
  apiKey: 000999888111
  • 1.
  • 2.
  • 3.

4. 创建将读取这些配置的 Bean

现在我们已经创建了属性,在读取属性时我们需要考虑 2 个用例。

  • 读取一次性属性——在这种情况下,我们可能需要创建一个需要读取一次或不能与任何其他属性分类的属性。在这种情况下,您可以使用@Value 注解来读取它。
  • 读取一组属性——在我们的示例中,我们已经在“bungie”类别下确定了两个分组属性。这就是我更喜欢创建属性的方式。我不喜欢有孤儿属性,因此我们只会看到如何设置它们。以下示例将向我们展示如何创建 。 Java Bean/Configuration,它将能够读取我们的属性文件并填充对象。
package io.howtoarchitect.destinyclanwars.config;
 
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@ConfigurationProperties("bungie")
@Getter @Setter
public class BungieClientSettings {
    private String rootPath;
    private String apiKey;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

如果你观察上面的代码块,你会注意到一些事情:

  • 我们曾经@Configuration让 Spring 应用程序知道这是一个 bean,应该这样初始化
  • @Getter并且@Setter来自 Lombok 包,为我们提供了默认的 Getter 和 Setter。这些是强制性的,因为 Spring 应用程序总是需要这些 getter 和 setter。
  • @ConfigurationProperties是这里的主要技巧的注释。它将遍历我们上下文中可用的所有属性,并搜索任何已映射的用户“bungie”。找到后,此注释将映射 YAML/Properties 文件值并将它们添加到我们的字符串中。

5. 消费属性

完成此设置后,最后一步是在我们的应用程序中读取这些属性。我们将在另一个 Spring bean/service 中读取这些属性。

package io.howtoarchitect.destinyclanwars.bungieclient;
 
import io.howtoarchitect.destinyclanwars.config.BungieClientSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
 
@Service
public class BungieClient {
    private static WebClient client;
 
    @Autowired
    private BungieClientSettings bungieClientSettings;
 
    public WebClient getDefaultClient() {
        client = WebClient.builder()
                .baseUrl(bungieClientSettings.getRootPath())
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .defaultHeader("X-API-KEY", bungieClientSettings.getApiKey())
                .build();
 
        return client;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

您会注意到我已将其添加BungieClientSettings为@Autowired依赖项。这会在我的类中注入 bean,当我需要访问这些属性时,我需要做的就是bungieClientSettings.getAPIKey()和bungieClientSettings.getRootPath()

结论

这就是您需要做的就是将您的属性外部化。尽早设置这一点很重要,因为如果不这样做,您最终会将其中许多分散在课堂上,并且迁移到多个环境将变得困难。