这应该很容易,但不起作用。 我在 Spring Boot (2.0.1-RELEASE) 应用程序中激活了 spring-boot-actuator。
Actuator-Endpoint /actuator/info
按预期工作,并且还显示正确的版本信息。文件 build-info.properties
存在。
当尝试使用以下方式访问版本属性时(例如在我的 Spring-Controller-Class 中):
@Value("${build.version}) private String version;
操作失败并出现错误无法解析值“${build.version}”中的占位符“build.version”
。
有什么建议吗?
最佳答案
使用 Spring 表达式语言,它非常简单干净。
@Value("#{buildProperties.get('version')}") // not 'build.version'
private String myAppBuildVersion;
或者更好的是,将 buildProperties
bean 直接Autowire
连接到您的组件,以便您可以根据需要使用它。
@Autowired
private BuildProperties buildProperties;
NOTE: The
autoconfiguration
strips off thebuild.
prefix. So yourSpEL
expressions should useversion
as key. Notbuild.version
.
关于java - 使用 @Value 访问 Spring Boot Actuator build.version 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50186970/