SpringBoot自定义配置自动提示

SpringBoot项目配置文件中如果使用自定义配置时IDE工具时不会自动提示的,如果想实现自动提示可按如下操作

编写自定义配置类

使用注解@ConfigurationProperties并指定前缀

1
2
3
4
5
6
7
8
9
10
11
@Component
@ConfigurationProperties(prefix = "zg.river")
@Data
public class RiverGlobalProperties {

private String notAllowRefreshIndex;

private String traceInterTime;

private String patrolMaxTime;
}

编写对应的配置文件

1
2
3
4
zg:
river:
trace-inter-time: 5
patrol-max-time: 10

添加注解处理器

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

修改IDEA配置

Settings –> Annotation Processor –> 勾选 Enable annotation processing

编译生成提示文件

  • 重新编译代码
  • 生成的文件如下classes/META-INF/spring-configuration-metadata.json
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    {
    "hints": [],
    "groups": [
    {
    "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
    "name": "zg.river",
    "type": "com.zg.river.config.properties.RiverGlobalProperties"
    }
    ],
    "properties": [
    {
    "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
    "name": "zg.river.not-allow-refresh-index",
    "description": "notAllowRefreshIndex",
    "type": "java.lang.String"
    },
    {
    "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
    "name": "zg.river.patrol-max-time",
    "description": "patrolMaxTime",
    "type": "java.lang.String"
    },
    {
    "sourceType": "com.zg.river.config.properties.RiverGlobalProperties",
    "name": "zg.river.trace-inter-time",
    "description": "traceInterTime",
    "type": "java.lang.String"
    }
    ]
    }
  • 之后发现自定义的配置可以自动提示,并且可以进行跳转了

    参考资料:

  • Configuration Metadata