解決spring-boot-configuration-processor對maven子模塊不起作用


環境
idea 2021.1
maven 3.6.1
springboot 2.3.10.RELEASED

問題: spring boot configuration annotation processor not configured

單模塊maven項目

pom內添加以下依賴即可消除警告

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

多模塊且喊子模塊maven項目

在父module的pom內添加以下依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <!-- <optional>true</optional> 不注釋掉子模塊無法引用到此依賴 -->
        </dependency>

然后在maven-compiler-plugin內的annotationProcessorPaths中添加相應path

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <target>${maven.compiler.target}</target>
                    <source>${maven.compiler.source}</source>
                    <encoding>UTF-8</encoding>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                            <version>${spring-boot.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

這樣就能消除警告啦,至於自定義yml或properties的內容快捷提示且能跳轉相應配置類,可以看如下簡單demo

demo

application.yml

my:
  a:
    name: lisi
    age: 11
    person:
      age: 12
      name: zhangsan

MyConfig.java

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

/**
 * <p>
 *     demo
 * </p>
 *
 * @author wandoupeas
 * @date 2021-09-16 11:48 上午
 */
@Data
@Component
@ConfigurationProperties(prefix = "my.a")
public class MyConfig {
    private String name;
    private String age;
    private MyConfigName person;
}

MyConfigName.java

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

/**
 * <p>
 *     demo
 * </p>
 *
 * @author wandoupeas
 * @date 2021-09-16 11:48 上午
 */
@Data
@Component
@ConfigurationProperties(prefix = "my.a.person")
public class MyConfigName {
    private String name = "zhangsan";

    private String age = "123";
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM