spring-boot-configuration-processor的简单使用(一)

1. 添加依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

2.编写自己的配置类

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

@Data
@Component
@ConfigurationProperties(prefix = "my.server")
public class MyServerProperties {
    private String name;
    private String ip = "127.0.0.1";
    private int port = 9797;
}

注意点:

如果不添加 @Component 注解,将会报错

Not registered via @EnableConfigurationProperties, marked as Spring component, or scanned via @ConfigurationPropertiesScan 

这段报错信息显示:该类既没有通过 @EnableConfigurationProperties 注册,将其标注为Spring组件,也没有通过  @ConfigurationPropertiesScan 被扫描

 

3.编译,生成metadata数据

4.配置文件中自动提示

 

posted @ 2022-09-15 15:55  pyt123456  阅读(803)  评论(0)    收藏  举报