Spring boot - Application.properties 中的自定义变量 [英] Spring boot - custom variables in Application.properties

查看:99
本文介绍了Spring boot - Application.properties 中的自定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 restful api 的 spring boot 客户端.我可以使用 application.properties 中的任何密钥条目,而不是在 java 类中硬编码 REST API 的 IP 地址?

I have spring boot client that consumes a restful api. Instead of hardcoding the IP address of the REST API in the java class, is there any key entry in the application.properties I can use?

如果没有,我可以创建一个自定义条目吗?

And if not, can I create a custom entry?

谢谢

推荐答案

Spring Boot 使用的基础设施可以在您自己的项目中以完全相同的方式使用.您在@zmitrok 回答中评论了未知属性"警告.那是因为您的属性没有元数据,所以 IDE 不知道它.

The infrastructure that Spring Boot uses can be used in your own project in the exact same way. You commented in @zmitrok answer about a "unknown property" warning. That is because your property has no meta-data so the IDE does not know about it.

如果可以的话,我强烈建议您不要使用 @Value,因为它与 Spring Boot 提供的相比相当有限(@Value 是 Spring 框架的一项功能).

I would strongly advice you not to use @Value if you can as it is rather limited compared to what Spring Boot offers (@Value is a Spring Framework feature).

首先为您的 IP 创建一些 POJO:

Start by creating some POJO for your IP:

@ConfigurationProperties("app.foo")
public class FooProperties {

    /**
     * IP of foo service used to blah.
     */
    private String ip = 127.0.0.1;

    // getter & setter
}

那么你有两个选择

  1. @Component 放在 FooProperties 上,并通过在任何 @Configuration<上添加 @EnableConfigurationProperties 来启用配置属性的处理/code> 类(从 Spring Boot 1.3.0.M3 开始,这最后一步不再需要)
  2. 保持 FooProperties 原样,并将 @EnableConfigurationProperties(FooProperties.class) 添加到您的任何 @Configuration 类中,这将创建一个 Spring Bean自动为你.
  1. Put @Component on FooProperties and enable the processing of configuration properties by adding @EnableConfigurationProperties on any of your @Configuration class (this last step is no longer necessary as of Spring Boot 1.3.0.M3)
  2. Leave FooProperties as is and add @EnableConfigurationProperties(FooProperties.class) to any of your @Configuration class which will create a Spring Bean automatically for you.

一旦你完成了 app.foo.ip 就可以在 application.properties 中使用并且你可以 @Autowired FooProperties 在代码中查找属性的值

Once you've done that app.foo.ip can be used in application.properties and you can @Autowired FooProperties in your code to look for the value of the property

@Component
public MyRestClient {

    private final FooProperties fooProperties;

    @Autowired
    public MyRestClient(FooProperties fooProperties) { ... }

    public callFoo() {
       String ip = this.fooProperties.getIp();
       ...
    }

}

好的,所以您的密钥在 IDE 中仍然是黄色的.最后一步是添加一个额外的将在构建时查看您的代码并生成相关元数据的依赖项.将以下内容添加到您的 pom.xml

Okay so your key is still yellow in your IDE. The last step is to add an extra dependency that will look your code and generate the relevant meta-data at build time. Add the following to your pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

瞧,您的密钥已被识别,您拥有 javadoc,IDE 为您提供默认值(您在字段上初始化的值).一旦确定,您就可以使用转换服务处理的任何类型(即 URL),并且该字段上的 javadoc 用于为您的密钥生成文档.

And voilà, your key is recognized, you have javadoc and the IDE gives you the default value (the value you initialized on the field). Once you've that you can use any type the conversion service handles (i.e. URL) and the javadoc on the field is used to generate documentation for your keys.

您还可以在您的字段上添加任何 JSR-303 约束验证(例如,一个正则表达式来检查它是一个有效的 ip).

You can also add any JSR-303 constraint validation on your field (for instance a regex to check it's a valid ip).

检查这个示例项目文档了解更多详情.

这篇关于Spring boot - Application.properties 中的自定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆