[英]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>>
行動:
更新應用程序的配置
現在,總結一下我已經做了什么來修復它:
@ConfigurationProperties
提供了注釋處理器:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>${spring-boot.version}</version> <optional>true</optional> </dependency>
我啟用了注釋處理器。 我正在使用 Intellij。 Annotation processors
-> Maven default annotation processors profile
已勾選Enable annotation processing
, Processor path:
包含(...)\\.m2\\repository\\org\\springframework\\boot\\spring-boot-configuration-processor\\2.0.2.RELEASE\\spring-boot-configuration-processor-2.0.2.RELEASE.jar
, Store generated sources relative to: Module content root
,
在 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>
CarsProperties
內的彈出CarsProperties
:重新運行 Spring Boot Configuration Annotation Processor 來更新生成的元數據
@EnableConfigurationProperties
為:Spring Boot 文檔說,每個項目都會自動包含@EnableConfigurationProperties。
Reimport All Maven Projects
、 clean install
、 Rebuild project
和Invalid Caches and Restart
我現在坐在那台電腦前幾個小時,無法完成。 我究竟做錯了什么 ? 為什么它不想工作?
將 spring boot 版本更新為2.1.6.RELEASE
並已修復
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.