image.png

image.png

我的解决

首先为什么其他的没有爆红呢,是因为其他的starter内部已经把这个spring-boot-configuration-processor和相关配置信息的文件配置好编译完了,所以idea能读取到,而我们自己定义的IDEA自然就无法读取,所以就报warning了

IDEA自动生成META-INF

如果交给IDEA用alt+Enter自动处理的话,会在resource下生成META-INF的文件夹,和在里面的additional-spring-configuration-metadata.json文件

image.png

并且会在文件内写入自动生成的配置项的解释信息

image.png

配置spring-boot-configuration-processor

但是,会提示未配置Spring Boot配置注释处理器

image.png

在这里打开文档就会跳转到Spring Boot官方文档,摘录如下:


载自spring boot官方文档的原文

https://docs.spring.io/spring-boot/docs/2.3.4.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor

3.1. Configuring the Annotation Processor
To use the processor, include a dependency on spring-boot-configuration-processor.

With Maven the dependency should be declared as optional, as shown in the following example:

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

If you have defined @ConfigurationProperties in your application, make sure to configure the spring-boot-maven-plugin to prevent the repackage goal from adding the dependency into the fat jar:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

With Gradle 4.5 and earlier, the dependency should be declared in the compileOnly configuration, as shown in the following example:

dependencies {
    compileOnly "org.springframework.boot:spring-boot-configuration-processor"
}

With Gradle 4.6 and later, the dependency should be declared in the annotationProcessor configuration, as shown in the following example:

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

If you are using an additional-spring-configuration-metadata.json file, the compileJava task should be configured to depend on the processResources task, as shown in the following example:

compileJava.inputs.files(processResources)

This dependency ensures that the additional metadata is available when the annotation processor runs during compilation.


我的解决方案

其实就是配置spring-boot-configuration-processor的过程,用Maven的,直接参考原文引入dependency就可以,我是用gradle的,对应的则是在dependencies中引入

annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

然后在gradle的最后加入

compileJava.inputs.files(processResources)

然后gradle重新同步,就会发现未配置Spring Boot配置注释处理器的提示没了

编写对应的@ConfigurationProperties类

其实完成了上一步,就会发现有些配置是没有关联到的,但其实,IDEA配置的additional-spring-configuration-metadata.json并不是配置文件里的选项的本体,@ConfigurationProperties类才是真正的正主,编译时由@ConfigurationProperties类自动生成spring-configuration-metadata.json,如果存在additional-spring-configuration-metadata.json,再对spring-configuration-metadata.json中的配置进行覆盖,不然,为啥叫“additional”呢。

所以IDEA生成的additional-spring-configuration-metadata.json并不是必须品,@ConfigurationProperties类才是

真的是被IDEA误导惨了

我要写的配置如下:
image.png

那么,我对应的@ConfigurationProperties类如下

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

@Data
@Component
@ConfigurationProperties("jwt")
public class JwtConfigProperties {

    private String header;

    private String secret;

    private String expiration;

    private String tokenHead;

    @Data
    @Component
    @ConfigurationProperties("jwt.route.authentication")
    public static class RouteAuthentication {

        private String path;

        private String refresh;

        private String register;
    }
}

写完后编译,就搞定啦(additional-spring-configuration-metadata.json可删)

image.png