SpringBoot 2.7.5中@Value注解无法从application.properties注入值排查
Spring Boot 2.7.5 @Value注入值为null问题排查
在Spring Boot 2.7.5项目中,于@Configuration类通过@Value("${file.name}")注解从application.properties注入配置值时,变量fileName被赋值为null。按预期逻辑,即使配置键不匹配,变量也应被赋值为字符串${file.name},而非null,说明项目存在异常。该功能在其他同版本Spring Boot项目中可正常工作,此项目异常,已完成以下排查:
- 确认
application.properties文件名拼写正确 - 确认使用
@Value的类已添加@Configuration注解 - 检查
application.properties中file.name键值对及键名拼写无误 - 确认Spring相关依赖库导入无问题
- 尝试添加
@PropertySource("classpath:foo.properties")进行测试 - 已正确导入
org.springframework.beans.factory.annotation.Value包 - 确认IDE已将resources文件夹标记为'Resources Root'
临时替代方案
使用原生Java Properties类手动读取配置:
private Properties props = new Properties(); props.load(VaultCredential.class.getResourceAsStream("/application.properties")); props.getProperty("file.name");
想请教此项目出现该异常的原因,以及还需排查哪些配置项?
内容的提问来源于stack exchange,提问作者Vinit More
相关产品推荐
相关产品推荐

