Spring boot - Application.properties 中的自定义变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32058814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:43:44  来源:igfitidea点击:

Spring boot - custom variables in Application.properties

springspring-mvcspring-bootspring-data

提问by Kingamere

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?

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

And if not, can I create a custom entry?

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

Thanks

谢谢

回答by Stephane Nicoll

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.

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

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

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

Start by creating some POJO for your IP:

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

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

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

    // getter & setter
}

Then you have two choices

那么你有两个选择

  1. Put @Componenton FooPropertiesand enable the processing of configuration properties by adding @EnableConfigurationPropertieson any of your @Configurationclass (this last step is no longer necessary as of Spring Boot 1.3.0.M3)
  2. Leave FooPropertiesas is and add @EnableConfigurationProperties(FooProperties.class)to any of your @Configurationclass which will create a Spring Bean automatically for you.
  1. @ComponentFooProperties,并通过添加使配置属性的处理@EnableConfigurationProperties上你的任何@Configuration类(这最后一步,不再是必要的,因为春天启动的1.3.0.M3
  2. 保持FooProperties原样并添加@EnableConfigurationProperties(FooProperties.class)到您的任何@Configuration类中,它将自动为您创建一个 Spring Bean。

Once you've done that app.foo.ipcan be used in application.propertiesand you can @AutowiredFooPropertiesin your code to look for the value of the property

完成后,app.foo.ip可以使用 inapplication.properties并且您可以@AutowiredFooProperties在代码中查找属性的值

@Component
public MyRestClient {

    private final FooProperties fooProperties;

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

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

}

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

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

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

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.

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

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

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

Check this sample projectand the documentationfor more details.

查看此示例项目文档以获取更多详细信息。

回答by cahen

Instead of hardcoding the IP into the properties file, you can start the application with

您可以使用以下命令启动应用程序,而不是将 IP 硬编码到属性文件中

-Dmy.property=127.127.10.20

-Dmy.property=127.127.10.20

And Spring Boot will automatically pick it up with

并且 Spring Boot 会自动拿起它

@Value("${my.property}")
private String myProperty;

回答by zmitrok

You can add your own entries to the application.properties. Just make sure that the property name does not clash with the common properties listed at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

您可以将自己的条目添加到 application.properties。只需确保属性名称不与http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties 中列出的通用属性冲突