一、场景
Spring Boot 项目的配置文件中,使用 自定义配置
时,会出现两个问题:
- 输入自定义配置时,IDE
没有自动提示
- 自定义配置,会出现
红色提示
官方提供的 spring-boot-starter
的配置项,用IDE配置的时候是有自动提示的。项目自定义的配置属性,在配置文件(yml 或 properties)中输入时,也希望能够自动提示。
二、解决方法:配置类绑定配置属性
核心步骤:
配置类
添加@ConfigurationProperties
注解- 添加
spring-boot-configuration-processor
依赖 - 编译项目,生成配置属性的元数据(
/META-INF/spring-configuration-metadata.json
) - 配置文件中,输入自定义配置,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 对象中。这使得配置管理变得更加简洁和易于维护。
下面是一个简单的例子来说明如何使用 @ConfigurationProperties
与 spring-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
路径时,就会返回配置文件中定义的 name
和 timeout
值。
以上就是使用 @ConfigurationProperties
和 spring-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
注解。这个处理器在编译时运行,可以帮助开发者更好地管理和验证配置属性。
主要功能
-
元数据生成:
- 自动生成配置属性的元数据(
.yml
或.json
文件),这些文件可以用来描述哪些配置属性是必需的、它们的数据类型以及默认值等信息。 - 这些元数据文件可以被 Spring Boot 自动发现并用于配置绑定过程中提供更好的错误提示和文档支持。
- 自动生成配置属性的元数据(
-
配置绑定验证:
- 在编译时检查
@ConfigurationProperties
类及其字段,确保所有配置项都是有效的,并且可以在运行时正确绑定到相应的 Java 对象中。 - 可以帮助提前发现配置绑定的问题,比如拼写错误或不存在的配置项。
- 在编译时检查
使用方法
要使用 spring-boot-configuration-processor
,你需要做以下几步:
-
添加依赖:
- 在你的项目构建配置文件(如
pom.xml
或build.gradle
)中添加spring-boot-configuration-processor
依赖。
- 在你的项目构建配置文件(如
-
应用注解:
- 在配置类上使用
@ConfigurationProperties
注解,并指定一个前缀来关联配置文件中的属性。
- 在配置类上使用