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>>
行动:
更新应用程序的配置
现在,总结一下我已经为修复它所做的工作:
- 根据文档,我添加了一个依赖项,它为我提供了注释处理器
@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>
- 此外,intellij 不断向我显示一个弹出窗口
CarsProperties
:
重新运行 Spring Boot 配置注释处理器以更新生成的元数据
- 我跳过
@EnableConfigurationProperties
了:
Spring Boot 文档说,每个项目都会自动包含@EnableConfigurationProperties。
- 介于两者之间的某个地方,我也做过:
Reimport All Maven Projects
、、、和clean install
Rebuild project
Invalid Caches and Restart
我现在坐在那台电脑前几个小时,无法完成它。我究竟做错了什么 ?为什么它不想工作?
解决方案
将 Spring Boot 版本更新为2.1.6.RELEASE
并已修复
推荐阅读
- python - Discord py后台任务不会向所有频道发送消息
- php - PHP preg_match_all 函数正则表达式
- c# - 选择选项卡时 TabControl 重新排序 ControlCollection
- swift - CaseItearble 的通用 next() 和 previous() 方法
- javascript - 如何在javascript中过滤数据
- ssl - 如何使用 java -Djavax.net.debug=true 选项启动 Leingen?
- c# - 绑定不起作用 - 没有属性、可绑定属性或找到事件错误
- python - Tf Summary 不给出直方图但保存会话图(预训练模型)
- c++ - 为什么不允许 std::variant 与其一种替代类型进行相等比较?
- python-3.x - 从文件中获取信息并创建字典