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

问题描述

我的Spring Boot客户端使用了一个宁静的api.不用在Java类中对REST API的IP地址进行硬编码,我可以使用application.properties中的任何键条目吗?

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 Framework功能)相比,它是相当有限的.

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. FooProperties上放置@Component并通过在任何@Configuration类上添加@EnableConfigurationProperties来启用配置属性的处理(从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.

完成后,可以在application.properties中使用app.foo.ip,并且可以在代码中@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>

voilà,您的密钥被识别,您拥有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).

检查此示例项目文档了解更多详细信息

Check this sample project and the documentation for more details.

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

08-11 05:01