簡體   English   中英

Spring boot——綁定屬性【配置屬性】

[英]Spring boot - binding properties [Configuration properties]

我在 Spring Boot 版本上工作: 2.0.2.RELEASE 在我的application.yml我有:

cars:
  color-to-brands:
    red: car1, car2
    blue: car3, car4

我的配置類如下所示:

@Getter @Setter
@Configuration
@ConfigurationProperties(prefix = "cars")
public class CarsProperties {

    private Map<String, List<String>> colorToBrands = Collections.emptyMap();
}

當我啟動應用程序時,我不斷收到:

無法將“cars.color-to-brands”下的屬性綁定到 java.util.Map>:

 Reason: Failed to bind properties under 'cars.color-to-brands' to java.util.Map<java.lang.String, java.util.List<java.lang.String>>

行動:

更新應用程序的配置

現在,總結一下我已經做了什么來修復它:

  1. 根據文檔,添加了一個依賴項,它為我的@ConfigurationProperties提供了注釋處理器:
 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>${spring-boot.version}</version> <optional>true</optional> </dependency>
  1. 啟用了注釋處理器 我正在使用 Intellij。 Annotation processors -> Maven default annotation processors profile已勾選Enable annotation processingProcessor path:包含(...)\\.m2\\repository\\org\\springframework\\boot\\spring-boot-configuration-processor\\2.0.2.RELEASE\\spring-boot-configuration-processor-2.0.2.RELEASE.jarStore generated sources relative to: Module content root

  2. 在 pom 文件中,我為此處理器添加了路徑(以及我使用的其他路徑):

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> </path> <path> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>${spring-boot.version}</version> </path> </annotationProcessorPaths> <compilerArgs> <compilerArg> -Amapstruct.defaultComponentModel=spring </compilerArg> </compilerArgs> </configuration>
  1. 此外,intellij 不斷向我展示CarsProperties內的彈出CarsProperties

重新運行 Spring Boot Configuration Annotation Processor 來更新生成的元數據

  1. 我跳過了@EnableConfigurationProperties為:

Spring Boot 文檔說,每個項目都會自動包含@EnableConfigurationProperties。

  1. 介於兩者之間,我也做了: Reimport All Maven Projectsclean installRebuild projectInvalid Caches and Restart

我現在坐在那台電腦前幾個小時,無法完成。 我究竟做錯了什么 ? 為什么它不想工作?

將 spring boot 版本更新為2.1.6.RELEASE並已修復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM