1. Overview
1.概述
When we’re injecting runtime properties into our Spring applications, we may define bean classes for groups of custom properties.
当我们在Spring应用程序中注入运行时属性时,我们可以为自定义属性组定义bean类。
IntelliJ provides help and auto-complete for the built-in properties beans. However, it needs a little help to provide these for custom properties.
IntelliJ为内置属性Bean提供帮助和自动完成。然而,它需要一点帮助来为自定义属性提供这些。
In this short tutorial, we’ll look at how to expose these properties to IntelliJ to make the development process easier.
在这个简短的教程中,我们将看看如何将这些属性暴露给IntelliJ,使开发过程更容易。
2. Custom Properties
2.自定义属性
Let’s have a look at the on-screen help IntelliJ can provide us regarding our application’s properties:
让我们来看看IntelliJ为我们提供的关于应用程序属性的屏幕帮助。
Here, the properties url and timeout-in-milliseconds are custom properties. We can see a description, type, and an optional default value.
这里,属性url和timeout-in-milliseconds是自定义属性。我们可以看到描述、类型和一个可选的默认值。
But, if a property is unknown, IntelliJ will show us a warning:
但是,如果一个属性是未知的,IntelliJ会向我们显示一个警告。
This is because, without metadata, IntelliJ cannot help us.
这是因为,没有元数据,IntelliJ无法帮助我们。
Now let’s take a look at what we have to do to fix this.
现在让我们来看看我们必须做什么来解决这个问题。
3. Dependencies
3.依赖性
First, we need to add the spring-boot-configuration-processor dependency to our pom.xml:
首先,我们需要将spring-boot-configuration-processor依赖性添加到我们的pom.xml。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
The spring-boot-configuration-processor is invoked each time we build our project. It will create the metadata files in target/classes/META-INF/.
每次我们构建项目时, spring-boot-configuration-processor都会被调用。它将在target/classes/META-INF/中创建元数据文件。
The dependency is marked as optional, which means that it is not inherited when somebody uses our project as a dependency.
该依赖关系被标记为可选的,这意味着当有人使用我们的项目作为依赖关系时,它不会被继承。
Next, we’ll see where the spring-boot-configuration-processor gets the information used to create the metadata.
接下来,我们将看到spring-boot-configuration-processor从哪里获得用于创建元数据的信息。
4. Configuration Metadata with @ConfigurationProperties
4.使用@ConfigurationProperties的配置元数据
We define our properties in a class annotated with @ConfigurationProperties:
我们在一个带有@ConfigurationProperties注释的类中定义我们的属性。
@Configuration
@ConfigurationProperties(prefix = "com.baeldung")
public class CustomProperties {
/**
* The url to connect to.
*/
String url;
/**
* The time to wait for the connection.
*/
private int timeoutInMilliSeconds = 1000;
// Getters and Setters
}
Here, the class contains the property names, their types, and any defaults provided in the initializer list. Also, the Javadoc provides descriptions of each property.
在这里,该类包含了属性名称,它们的类型,以及初始化器列表中提供的任何默认值。另外,Javadoc提供了每个属性的描述。
During a build, the annotation processor searches for all classes that are annotated with @ConfigurationProperties. It generates custom property metadata for each instance variable of the class.
在构建过程中,注释处理器会搜索所有被注释为 @ConfigurationProperties的类。它为该类的每个实例变量生成自定义属性元数据。
5. Configuration Metadata File
5.配置元数据文件
5.1. Format of the Metadata File
5.1.元数据文件的格式
The metadata file that describes the custom properties drives the contextual help in IntelliJ, for example:
例如,描述自定义属性的metadata文件可以驱动IntelliJ中的上下文帮助。
{
"groups": [
{
"name": "com.baeldung",
"type": "com.baeldung.configuration.processor.CustomProperties",
"sourceType": "com.baeldung.configuration.processor.CustomProperties"
}
],
"properties": [
{
"name": "com.baeldung.url",
"type": "java.lang.String",
"description": "The url to connect to.",
"sourceType": "com.baeldung.configuration.processor.CustomProperties"
},
{
"name": "com.baeldung.timeout-in-milli-seconds",
"type": "java.lang.Integer",
"description": "The time to wait for the connection.",
"sourceType": "com.baeldung.configuration.processor.CustomProperties",
"defaultValue": 1000
}
],
"hints": []
}
As the annotation processor generates this file for us from our code, there’s no need to look at or edit this file directly.
由于注释处理器从我们的代码中为我们生成了这个文件,所以没有必要直接查看或编辑这个文件。
5.2. Metadata Without a ConfigurationProperties Bean
5.2.没有ConfigurationProperties Bean的元数据
If we have existing properties that are not introduced by a @ConfigurationProperties, but still want their metadata file, then IntelliJ can help.
如果我们有现有的属性没有被@ConfigurationProperties引入,但仍然想要它们的元数据文件,那么IntelliJ可以帮忙。
Let’s take a closer look at the warning message from before:
让我们仔细看一下之前的警告信息。
Here we see a Define configuration key option, which we can use to create an additional-spring-configuration-metadata.json file. The created file will look like:
这里我们看到一个定义配置键选项,我们可以用它来创建一个additional-spring-configuration-metadata.json文件。创建的文件将看起来像。
{
"properties": [
{
"name": "com.baeldung.timeoutInMilliSeconds",
"type": "java.lang.String",
"description": "Description for com.baeldung.timeoutInMilliSeconds."
}
]
}
As there’s no information about the property from anywhere else, we’ll have to manually edit the metadata inside it. The default type is always String.
由于其他地方没有关于该属性的信息,我们将不得不手动编辑里面的元数据。默认的类型总是s String。
Let’s put some extra information into the file:
让我们把一些额外的信息放到文件中。
{
"properties": [
{
"name": "com.baeldung.timeout-in-milli-seconds",
"type": "java.lang.Integer",
"description": "The time to wait for the connection.",
"sourceType": "com.baeldung.configuration.processor.CustomProperties",
"defaultValue": 1000
}
]
}
Note that we’ll need to rebuild the project to see the new property come up in auto-complete.
请注意,我们需要重建项目以看到新属性出现在自动完成中。
Also, we should note that the option to generate this metadata file is also available via IntelliJ’s Alt+ENTER shortcut over an unknown property.
另外,我们应该注意到,生成这个元数据文件的选项也可以通过IntelliJ的Alt+ENTER快捷键在一个未知属性上使用。
6. Conclusion
6.结论
In this article, we looked at how IntelliJ uses the configuration property metadata to provide help with our properties files.
在这篇文章中,我们研究了IntelliJ如何使用配置属性元数据来为我们的属性文件提供帮助。
We saw how to use Spring’s annotation processor to generate the metadata from custom classes. Then, we saw how to use a shortcut in IntelliJ to create a metadata file to edit manually.
我们看到了如何使用Spring的注解处理器来生成自定义类的元数据。然后,我们看到了如何使用IntelliJ中的快捷方式来创建一个元数据文件进行手动编辑。
As always, the code from the examples in this article can be found over on GitHub.
一如既往,本文中的例子的代码可以在GitHub上找到。