Spring Native构建中含HashMap的配置属性类绑定失败排查
GraalVM + Spring Boot 3.0原生镜像构建时HashMap配置属性绑定失败问题
我尝试使用GraalVM与Spring Boot 3.0构建原生可执行文件,但当配置属性类中包含HashMap时,应用启动失败。
配置属性类代码
@Validated @Data @Component @ConfigurationProperties(prefix = "test-props") @FieldDefaults(level = AccessLevel.PRIVATE) public class TestProperties { @NotEmpty HashMap<String, PermissionsConfig> datagroups; @Data @Builder @AllArgsConstructor @NoArgsConstructor @FieldDefaults(level = AccessLevel.PRIVATE) public static class PermissionsConfig { // 字段 } }
配置文件内容
test-props: datagroups: data: read: code: READ_DATA urlPattern: - /some/pattern/
测试运行器代码
@Slf4j @Component @RequiredArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) public class TestRunner implements ApplicationRunner { TestProperties testProperties; @Override public void run(ApplicationArguments args) throws Exception { if (testProperties.getDatagroups() == null) { throw new RuntimeException("property not binded"); } log.info("property {}", testProperties); } }
报错信息
APPLICATION FAILED TO START
Description:
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'test-props' to test.properties.TestProperties failed:
Property: test-props.datagroups
Value: "null"
以普通Java应用启动时一切正常,但构建为原生可执行文件后启动报错。我已尝试为属性类添加额外的提示(hints),此前连验证都无法正常工作。我预期该功能在原生镜像中也能正常运行,请问我忽略了什么?
复现步骤
- 获取仓库代码
- 执行命令:
./gradlew nativeCompile - 运行可执行文件:
./build/native/nativeCompile/app
内容的提问来源于stack exchange,提问作者Jonathan H
相关产品推荐
相关产品推荐

