温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot中怎么配置元数据

发布时间:2021-06-18 16:08:51 来源:亿速云 阅读:260 作者:Leah 栏目:大数据

今天就跟大家聊聊有关Spring Boot中怎么配置元数据,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

依赖

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

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

<dependency>    
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-configuration-processor</artifactid>
    <version>2.1.7.RELEASE</version>
    <optional>true</optional>
</dependency>

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

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

4. 配置属性示例

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

@Configuration
@ConfigurationProperties(prefix = "database")
public class DatabaseProperties {
    
    public static class Server {
        private String ip;
        private int port;
        
        // standard getters and setters
    }

    private String username;
    private String password;
    private Server server;
    
    // standard getters and setters
}

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

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

#Simple Properties
database.username=baeldung
database.password=password

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

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AnnotationProcessorApplication.class)
@TestPropertySource("classpath:databaseproperties-test.properties")
public class DatabasePropertiesIntegrationTest {

    @Autowired
    private DatabaseProperties databaseProperties;

    @Test
    public void whenSimplePropertyQueriedThenReturnsPropertyValue() 
      throws Exception {
        Assert.assertEquals("Incorrectly bound Username property", 
          "baeldung", databaseProperties.getUsername());
        Assert.assertEquals("Incorrectly bound Password property", 
          "password", databaseProperties.getPassword());
    }
    
}

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

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

@Test
public void whenNestedPropertyQueriedThenReturnsPropertyValue() 
  throws Exception {
    Assert.assertEquals("Incorrectly bound Server IP nested property",
      "127.0.0.1", databaseProperties.getServer().getIp());
    Assert.assertEquals("Incorrectly bound Server Port nested property", 
      3306, databaseProperties.getServer().getPort());
}

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

5. 生成配置元数据

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

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

{
  "groups": [
    {
      "name": "database",
      "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
    },
    {
      "name": "database.server",
      "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
      "sourceMethod": "getServer()"
    }
  ],
  "properties": [
    {
      "name": "database.password",
      "type": "java.lang.String",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
    },
    {
      "name": "database.server.ip",
      "type": "java.lang.String",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"
    },
    {
      "name": "database.server.port",
      "type": "java.lang.Integer",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
      "defaultValue": 0
    },
    {
      "name": "database.username",
      "type": "java.lang.String",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
    }
  ],
  "hints": []
}

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

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

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

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

public static class Server {

    /**
     * The IP of the database server
     */
    private String ip;

    /**
     * The Port of the database server.
     * The Default value is 443.
     * The allowed values are in the range 400-4000.
     */
    @Min(400)
    @Max(800)
    private int port = 443;

    // standard getters and setters
}

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

{
  "groups": [
    {
      "name": "database",
      "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
    },
    {
      "name": "database.server",
      "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
      "sourceMethod": "getServer()"
    }
  ],
  "properties": [
    {
      "name": "database.password",
      "type": "java.lang.String",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
    },
    {
      "name": "database.server.ip",
      "type": "java.lang.String",
      "description": "The IP of the database server",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"
    },
    {
      "name": "database.server.port",
      "type": "java.lang.Integer",
      "description": "The Port of the database server. The Default value is 443.
        The allowed values are in the range 400-4000",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
      "defaultValue": 443
    },
    {
      "name": "database.username",
      "type": "java.lang.String",
      "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
    }
  ],
  "hints": []
}

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

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

5.2. 理解元数据格式

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

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

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

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

看完上述内容,你们对Spring Boot中怎么配置元数据有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI