1. 引入依赖

在 pom.xml 文件中加入以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
  1. 编写配置类

在 Spring Boot 项目中,我们可以通过在 application.yml 或 application.properties 文件中添加配置项来配置应用程序。但是,如果我们需要自定义一些配置项,怎么办呢?我们可以编写一个配置类来实现自定义配置。

@ConfigurationProperties(prefix = "my")
public class MyConfig {
    private String name;
    private int age;
    // getter 和 setter

}

在上面的代码中,我们使用 @ConfigurationProperties 注解来指定配置属性的前缀,然后使用 getter 和 setter 方法来获取和设置属性值。

  1. 编写配置文件

在 application.yml 或 application.properties 文件中,我们可以为自定义的配置项设置默认值。

my:
  name: Tom
  age: 18
  1. 开启注解处理器

在 IDEA 中,我们需要开启注解处理器才能让自定义的属性 yml,properties 配置文件有提示。在 pom.xml 文件中添加以下配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <requiresUnpack>
                            <dependency>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </dependency>
                        </requiresUnpack>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>kapt</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>kapt</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <sourceDirs>
                    <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                    <sourceDir>${project.basedir}/src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessors>
                    <annotationProcessor>org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor</annotationProcessor>
                </annotationProcessors>
            </configuration>
        </plugin>
    </plugins>
</build>

在上面的配置中,我们开启了注解处理器,并指定了需要处理的注解处理器类。

  1. 验证

配置文件的提示已经可以使用,我们可以通过在代码中使用 @Value 注解来获取配置项的值。

@RestController
public class MyController {
    @Value("${my.name}")
    private String name;

    @Value("${my.age}")
    private int age;

    @GetMapping("/")
    public String hello() {
        return "Hello " + name + ", you are " + age + " years old.";
    }
}

然后我们启动应用程序,访问 http://localhost:8080/,应该可以看到类似下面的输出:

Hello Tom, you are 18 years old.
SpringBoot 如何让自定义的属性 ymlproperties配置文件有提示

原文地址: https://www.cveoy.top/t/topic/bECI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录