Java application.properties 中的 SpringBoot 未知属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43562880/
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-08-12 01:00:35  来源:igfitidea点击:

SpringBoot unknown property in application.properties

javaspringspring-mvcspring-bootproperties-file

提问by Nu?ito de la Calzada

I've generated a Spring Boot web application using Spring Initializr, using embedded Tomcat + Thymeleaf template engine.

我使用 Spring Initializr 生成了一个 Spring Boot Web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎。

I put this property in my application.properties

我把这个属性放在我的 application.properties 中

[email protected]

I am using Spring Tool Suite Version: 3.8.4.RELEASE as a development environment, but I got this warning in the Editor 'default.to.address' is an unknown property.

我使用 Spring Tool Suite Version: 3.8.4.RELEASE 作为开发环境,但我在编辑器中收到此警告 'default.to.address' is an unknown property.

Should I put this property in another property file ?

我应该把这个属性放在另一个属性文件中吗?

采纳答案by Darren Forsythe

It's because it's being opened by the STS properties editor which validates properties amongst other things. There's no harm in having it in the application.properties file, you can even add your own meta-data for the property.

这是因为它是由 STS 属性编辑器打开的,它验证属性等。将它放在 application.properties 文件中没有坏处,您甚至可以为该属性添加自己的元数据。

http://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html

http://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html

回答by Takyon

I use this method to add properties in the file applciation.properties.

我使用此方法在文件 applciation.properties 中添加属性。

Add your new property in applciation.properties : [email protected] Hover the new property, you'll see a "quickfixes tooltip" which proposes you to add the new property : Create metadata for 'default.to.address'.

在 applciation.properties 中添加您的新属性:[email protected] 将鼠标悬停在新属性上,您将看到一个“快速修复工具提示”,建议您添加新属性:为“默认”创建元数据。讲话'。

Then, browse the class and field you want to bind the property to and add this annotation :

然后,浏览您要将属性绑定到的类和字段并添加此注释:

@Value("${default.to.address}")
private String address;

Now your object field should be valued with the property value.

现在您的对象字段应该使用属性值进行赋值。

回答by Sakthivel thangasamy

You should try adding these kind of values in Environment, instead application.properties, since you have the option to update the values anytime without doing compile changes / redeploy changes. application.properties could more beneficial for the properties that you never change like database credentials.

您应该尝试在 Environment 中添加这些类型的值,而不是 application.properties,因为您可以随时更新这些值,而无需进行编译更改/重新部署更改。application.properties 可能更有益于您永远不会更改的属性,例如数据库凭据。

回答by Akash

I was also having the same warnings in application.properties and looking for a way to get rid of this. Searching for an answer has led me here. So I am posting my answer; it may be useful.

我在 application.properties 中也有同样的警告,并正在寻找一种方法来摆脱这个。寻找答案让我来到这里。所以我发布了我的答案;它可能有用。

There is no harm in using your custom properties in application.properties. There are two ways to get rid of this -

在 application.properties 中使用自定义属性没有任何害处。有两种方法可以摆脱这种情况 -

  1. As mentioned in one of the answers, you can add the meta-data for the custom properties (manually or using quick-fix in STS).

  2. If you don't want to add meta-data, then in STS, go to Window -> preferences -> spring -> boot -> properties editor. Here, select 'unknown property' as ignore. By default, it is warning.

  1. 如其中一个答案所述,您可以为自定义属性添加元数据(手动或使用 STS 中的快速修复)。

  2. 如果您不想添加元数据,那么在 STS 中,转到窗口 -> 首选项 -> spring -> 启动 -> 属性编辑器。在这里,选择 'unknown property' 作为 ignore。默认情况下,它是警告。

回答by Hussam

You need to use spring-boot-configuration-processorwhich will generate the configuration metadata for you.

您需要使用spring-boot-configuration-processorwhich 将为您生成配置元数据。

See the example: https://www.baeldung.com/spring-boot-configuration-metadata

看例子:https: //www.baeldung.com/spring-boot-configuration-metadata