经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Boot 配置元数据指南
来源:cnblogs  作者:锅外的大佬  时间:2019/10/24 10:11:53  对本文有异议

1. 概览

在编写 Spring Boot 应用程序时,将配置属性映射到 Java bean 上是非常有用的。但是,记录这些属性的最好方法是什么呢?

在本教程中,我们将探讨 Spring Boot Configuration Processor关联的 JSON 元数据文件,该 JSON 文档记录每个属性的含义、约束等。

2. 配置元数据

作为开发人员,我们开发的大多数应用程序在某种程度上必须是可配置的。但是在通常情况下,我们并不能够真正的理解配置参数的作用,比如它有默认值,又或者是过时的,有时我们甚至不知道该属性的存在。

为了帮助我们理清楚,Spring Boot 生成了配置元数据的 JSON 文件,为我们提供关于如何使用属性的有用信息。所以,配置元数据是一个描述性文件,它包含与配置属性交互所需的必要信息。

这个文件的好处是 IDE 也能读懂它,从而为我们提供自动完成 Spring 属性配置的工作,以及其他配置提示。

3. 依赖

为了生成此配置元数据,我们将使用 spring-boot-configuration-processor 的依赖.

因此,让我们继续将依赖项添加为可选依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-configuration-processor</artifactId>
  4. <version>2.1.7.RELEASE</version>
  5. <optional>true</optional>
  6. </dependency>

这种依赖关系将为我们提供在构建项目时调用的 Java 注解处理器。我们稍后会详细讨论这个问题。

为了防止 @ConfigurationProperties 不应用于我们的项目使用的其他模块,在 Maven 中添加依赖项为可选依赖 是最好的做法。

4. 配置属性示例

现在来研究处理器是怎么工作的,我们需要使用 Java bean 获取在 Spring Boot 应用程序中包含一些属性:

  1. @Configuration
  2. @ConfigurationProperties(prefix = "database")
  3. public class DatabaseProperties {
  4. public static class Server {
  5. private String ip;
  6. private int port;
  7. // standard getters and setters
  8. }
  9. private String username;
  10. private String password;
  11. private Server server;
  12. // standard getters and setters
  13. }

要做到这一点,我们可以使用 @ConfigurationProperties 注解。配置处理器会扫描使用了此注解的类和方法,用来访问配置参数并生成配置元数据。

让我们将这些属性中添加到属性文件中。在示例中,我们把文件命名为 databaseproperties-test.properties

  1. #Simple Properties
  2. database.username=baeldung
  3. database.password=password

我们还将添加一个测试,以确保我们都做对了:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(classes = AnnotationProcessorApplication.class)
  3. @TestPropertySource("classpath:databaseproperties-test.properties")
  4. public class DatabasePropertiesIntegrationTest {
  5. @Autowired
  6. private DatabaseProperties databaseProperties;
  7. @Test
  8. public void whenSimplePropertyQueriedThenReturnsPropertyValue()
  9. throws Exception {
  10. Assert.assertEquals("Incorrectly bound Username property",
  11. "baeldung", databaseProperties.getUsername());
  12. Assert.assertEquals("Incorrectly bound Password property",
  13. "password", databaseProperties.getPassword());
  14. }
  15. }

我们通过内部类 Server 还添加了嵌套属性 database.server.iddatabase.server.port我们应该添加内部类 Server 以及一个 server 的属性并且生成他的 getter 和 setter 方法。

在我们的测试中,让我们快速检查一下,确保我们也可以成功地设置和读取嵌套属性:

  1. @Test
  2. public void whenNestedPropertyQueriedThenReturnsPropertyValue()
  3. throws Exception {
  4. Assert.assertEquals("Incorrectly bound Server IP nested property",
  5. "127.0.0.1", databaseProperties.getServer().getIp());
  6. Assert.assertEquals("Incorrectly bound Server Port nested property",
  7. 3306, databaseProperties.getServer().getPort());
  8. }

好了,现在我们准备好来使用处理器了。

5. 生成配置元数据

我们在前面提到过,配置处理器生成一个文件 – 它是使用注解处理实现的。

所以,在编译我们的项目之后,我们将在目录 target/classes/META-INF 下看到文件名为 spring-configuration-metadata.json 的文件:

  1. {
  2. "groups": [
  3. {
  4. "name": "database",
  5. "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
  6. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  7. },
  8. {
  9. "name": "database.server",
  10. "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
  11. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
  12. "sourceMethod": "getServer()"
  13. }
  14. ],
  15. "properties": [
  16. {
  17. "name": "database.password",
  18. "type": "java.lang.String",
  19. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  20. },
  21. {
  22. "name": "database.server.ip",
  23. "type": "java.lang.String",
  24. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"
  25. },
  26. {
  27. "name": "database.server.port",
  28. "type": "java.lang.Integer",
  29. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
  30. "defaultValue": 0
  31. },
  32. {
  33. "name": "database.username",
  34. "type": "java.lang.String",
  35. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  36. }
  37. ],
  38. "hints": []
  39. }

接下来,让我们看看更改 Java bean 上的注解如何影响元数据。

5.1. 关于配置元数据的其他信息

首先,让我们将 JavaDoc 注释添加到Server 上.

第二,让我们给出一个 database.server.port 字段的默认值并最后添加 @Min@Max 注解:

  1. public static class Server {
  2. /**
  3. * The IP of the database server
  4. */
  5. private String ip;
  6. /**
  7. * The Port of the database server.
  8. * The Default value is 443.
  9. * The allowed values are in the range 400-4000.
  10. */
  11. @Min(400)
  12. @Max(800)
  13. private int port = 443;
  14. // standard getters and setters
  15. }

如果我们检查 spring-configuration-metadata.json 文件,我们将看到这些额外的信息得到了反映:

  1. {
  2. "groups": [
  3. {
  4. "name": "database",
  5. "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
  6. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  7. },
  8. {
  9. "name": "database.server",
  10. "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
  11. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
  12. "sourceMethod": "getServer()"
  13. }
  14. ],
  15. "properties": [
  16. {
  17. "name": "database.password",
  18. "type": "java.lang.String",
  19. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  20. },
  21. {
  22. "name": "database.server.ip",
  23. "type": "java.lang.String",
  24. "description": "The IP of the database server",
  25. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"
  26. },
  27. {
  28. "name": "database.server.port",
  29. "type": "java.lang.Integer",
  30. "description": "The Port of the database server. The Default value is 443.
  31. The allowed values are in the range 400-4000",
  32. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
  33. "defaultValue": 443
  34. },
  35. {
  36. "name": "database.username",
  37. "type": "java.lang.String",
  38. "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  39. }
  40. ],
  41. "hints": []
  42. }

我们可以找到 database.server.ipdatabase.server.port 属性的不同之处。事实上,额外的信息是非常有帮助的。开发人员和 IDE 都更容易理解每个属性的功能。

我们还应该确保触发构建以获得更新的文件。在Eclipse中,如果我们检查自动生成选项时,每个保存操作都将触发生成。在 IntelliJ 中,我们应该手动触发构建。

5.2. 理解元数据格式

让我们仔细看看 JSON 元数据文件,并讨论其组成。

Groups 是用于分组其他属性的较高级别的项,而不指定值本身。在我们的例子中,我们有数据库组,它也是配置属性的前缀。我们还有一个 database 组,它是通过内部类把 IPport 属性作为一个组。

属性是可以为其指定值的配置项。这些属性配置在后缀为 .properties或 .yml* 文件中,并且可以有额外的信息,比如默认值和验证,就像我们在上面的示例中看到的那样。

提示是帮助用户设置属性值的附加信息。例如,如果我们有一组属性的允许值,我们可以提供每个属性的描述。IDE 将为这些提示提供自动选择的帮助。

配置元数据上的每个组成都有自己的属性。来解释配置属性的详细用法。

6. 总结

在本文中,我们介绍了 Spring Boot 配置处理器及其创建配置元数据的功能。使用元数据可以使与配置参数的交互变得更加容易。

我们给出了一个生成的配置元数据的示例,并详细解释了它的格式和组成。

我们还看到了 IDE 上的自动完成支持是多么有帮助。

与往常一样,本文中提到的所有代码片段都可以在我们的 GitHub 存储库

原文:https://www.baeldung.com/spring-boot-configuration-metadata

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号