Spring Boot-绑定属性[配置属性]

时间:2020-03-01 19:12:13

标签: spring spring-boot

我正在使用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. According to documentation添加了一个依赖项,该依赖项 我的@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内的弹出窗口:

重新运行Spring Boot配置注释处理器以进行更新 生成的元数据

  1. 我已将@EnableConfigurationProperties跳过为:

Spring Boot documentation说,每个项目都会自动包含 @EnableConfigurationProperties。

  1. 我之间的某个地方也做过:Reimport All Maven Projectsclean installRebuild projectInvalid Caches and Restart

我现在在那台计算机前坐了几个小时,无法完成它。我究竟做错了什么 ?为什么它不想工作?

1 个答案:

答案 0 :(得分:1)

已将Spring Boot版本更新为2.1.6.RELEASE,并且已修复