SpringBoot自定义配置自动提示,避免报红:@ConfigurationProperties、spring-boot-configuration-processor、编译生成元数据

一、场景

Spring Boot 项目的配置文件中,使用 自定义配置 时,会出现两个问题:

  1. 输入自定义配置时,IDE 没有自动提示
  2. 自定义配置,会出现 红色提示

官方提供的 spring-boot-starter 的配置项,用IDE配置的时候是有自动提示的。项目自定义的配置属性,在配置文件(yml 或 properties)中输入时,也希望能够自动提示。

二、解决方法:配置类绑定配置属性

核心步骤:

  1. 配置类 添加 @ConfigurationProperties 注解
  2. 添加 spring-boot-configuration-processor 依赖
  3. 编译项目,生成配置属性的元数据(/META-INF/spring-configuration-metadata.json
  4. 配置文件中,输入自定义配置,IDE自动提示补全,且不会红色提示

使用 @ConfigurationProperties 注解 + spring-boot-configuration-processor 依赖,编译时自动生成配置属性的元数据(/META-INF/spring-configuration-metadata.json)。

三、核心示例

在这里插入图片描述

四、自动提示

使用配置类绑定配置属性后,项目自定义的配置属性,能够自动提示。

在这里插入图片描述

五、示例代码

(1)配置类:HelloWorldProperties

package com.example.hello_feign_client.core.property;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "hello-world")
public class HelloWorldProperties {
    private String url;
}

(2)spring-boot-configuration-processor

在 pom.xml 文件中添加如下依赖:

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

(3)元数据:/META-INF/spring-configuration-metadata.json

配置属性的元数据,是在编译时自动生成的。

spring-boot-configuration-processor,根据 HelloWorldProperties 配置类(@ConfigurationProperties注解),生成的下面的元数据。

{
  "groups": [
    {
      "name": "hello-world",
      "type": "com.example.hello_feign_client.core.property.HelloWorldProperties",
      "sourceType": "com.example.hello_feign_client.core.property.HelloWorldProperties"
    }
  ],
  "properties": [
    {
      "name": "hello-world.url",
      "type": "java.lang.String",
      "sourceType": "com.example.hello_feign_client.core.property.HelloWorldProperties"
    }
  ],
  "hints": []
}

(4)配置文件:application.yml

hello-world:
  url: http://localhost:8080

六、原理详解(AI问答,仅做参考)

SpringBoot,使用配置类绑定配置属性(@ConfigurationProperties注解 + spring-boot-configuration-processor依赖)

在 Spring Boot 中,@ConfigurationProperties 注解被用来方便地将配置文件中的属性绑定到 Java 对象中。这使得配置管理变得更加简洁和易于维护。

下面是一个简单的例子来说明如何使用 @ConfigurationPropertiesspring-boot-configuration-processor 依赖进行配置绑定。

步骤 1: 添加依赖

首先,你需要确保你的项目中有 spring-boot-configuration-processor 这个依赖。如果你使用的是 Maven,请在 pom.xml 文件中添加如下依赖:

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

如果你使用的是 Gradle,则可以添加以下依赖:

dependencies {
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
}

步骤 2: 创建配置属性类

接下来创建一个 Java 类来存放配置属性。例如,我们创建一个 AppProperties 类:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {

    private String name;
    private int timeout;
    private Credentials credentials;

    // Getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public Credentials getCredentials() {
        return credentials;
    }

    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
    }

    // Nested class for credentials
    public static class Credentials {
        private String username;
        private String password;

        // Getters and setters
        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}

这里定义了一个主类 AppProperties 和一个嵌套类 Credentials,并指定了 prefix = "app",这意味着配置文件中的所有以 app 开头的键都会被绑定到这个类的实例上。

步骤 3: 配置文件

application.properties 或者 application.yml 文件中添加相应的配置:

app.name=MyApp
app.timeout=3000
app.credentials.username=admin
app.credentials.password=secret

或者在 YAML 文件中:

app:
  name: MyApp
  timeout: 3000
  credentials:
    username: admin
    password: secret

步骤 4: 使用配置属性

你可以在任何地方注入 AppProperties 类来访问配置值:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    private final AppProperties appProperties;

    @Autowired
    public MyController(AppProperties appProperties) {
        this.appProperties = appProperties;
    }

    @GetMapping("/config")
    public String getConfig() {
        return "Name: " + appProperties.getName() + ", Timeout: " + appProperties.getTimeout();
    }
}

这样,当你访问 /config 路径时,就会返回配置文件中定义的 nametimeout 值。

以上就是使用 @ConfigurationPropertiesspring-boot-configuration-processor 进行配置绑定的基本步骤。这种方法能够帮助你更好地组织和管理应用配置。

七、spring-boot-configuration-processor

spring-boot-configuration-processor 是Spring Boot提供的一种用于处理注解的功能,通常与@ConfigurationProperties 注解一起使用。它在编译阶段运行,帮助生成一些元数据,这些元数据可以帮助Spring Boot更好地理解和处理配置属性。

例如,如果你有一个类使用了@ConfigurationProperties注解来绑定配置文件中的属性,那么spring-boot-configuration-processor可以在编译时检查这些属性,并确保它们正确地映射到Java类上。这有助于避免运行时错误,并提供更好的开发体验。

总的来说,spring-boot-configuration-processor是一个可选的依赖项,用于帮助Spring Boot在编译时期处理配置属性绑定相关的元数据。

spring-boot-configuration-processor(AI问答,仅做参考)

spring-boot-configuration-processor 是 Spring Boot 中的一个模块,主要用于处理 Spring Boot 应用程序中的 @ConfigurationProperties 注解。这个处理器在编译时运行,可以帮助开发者更好地管理和验证配置属性。

主要功能

  1. 元数据生成

    • 自动生成配置属性的元数据(.yml.json 文件),这些文件可以用来描述哪些配置属性是必需的、它们的数据类型以及默认值等信息。
    • 这些元数据文件可以被 Spring Boot 自动发现并用于配置绑定过程中提供更好的错误提示和文档支持。
  2. 配置绑定验证

    • 在编译时检查 @ConfigurationProperties 类及其字段,确保所有配置项都是有效的,并且可以在运行时正确绑定到相应的 Java 对象中。
    • 可以帮助提前发现配置绑定的问题,比如拼写错误或不存在的配置项。

使用方法

要使用 spring-boot-configuration-processor,你需要做以下几步:

  1. 添加依赖

    • 在你的项目构建配置文件(如 pom.xmlbuild.gradle)中添加 spring-boot-configuration-processor 依赖。
  2. 应用注解

    • 在配置类上使用 @ConfigurationProperties 注解,并指定一个前缀来关联配置文件中的属性。

参考:yml自定义配置自动提示

yml自定义配置自动提示

Spring Boot Configuration Processor 是一个用于生成配置元数据的注解处理器,可以帮助我们在编译生成配置元数据,方便 IDE 自动生成配置属性的提示和文档,提高开发效率。 下面是使用 Spring Boot Configuration Processor 的步骤: 1. 添加 Maven 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> ``` 2. 在配置上添加 `@ConfigurationProperties` 注解,指定配置属性的前缀: ```java @ConfigurationProperties(prefix = "myconfig") public class MyConfig { private String name; private int age; // getters and setters } ``` 3. 在配置属性上添加 `@ConfigurationProperties` 注解,指定属性名和默认值: ```java public class MyConfig { @ConfigurationProperties("name") private String name = "default"; @ConfigurationProperties("age") private int age = 18; // getters and setters } ``` 4. 在配置同级目录下创建 `META-INF/spring-configuration-metadata.json` 文件,配置属性的元数据信息: ```json { "groups": [ { "name": "myconfig", "type": "com.example.demo.MyConfig", "sourceType": "com.example.demo.MyConfig" } ], "properties": [ { "name": "myconfig.name", "type": "java.lang.String", "description": "Name of the user", "defaultValue": "default" }, { "name": "myconfig.age", "type": "java.lang.Integer", "description": "Age of the user", "defaultValue": 18 } ] } ``` 5. 在 IDE 中使用配置属性时,会自动提示和补全属性名、型、描述和默认值等信息。 注意:在使用 `@ConfigurationProperties` 注解时,需要在配置上添加 `@EnableConfigurationProperties` 注解,或者在 Spring Boot 应用程序主上添加 `@EnableConfigurationProperties(MyConfig.class)` 注解,才能使属性生效。 参考文档:https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋冠巡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值