java - 使用 @Value 访问 Spring Boot Actuator build.version 属性

标签 java spring spring-boot spring-boot-actuator

这应该很容易,但不起作用。 我在 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 the build. prefix. So your SpEL expressions should use version as key. Not build.version.

关于java - 使用 @Value 访问 Spring Boot Actuator build.version 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50186970/

相关文章:

java - SpringBoot 2 与 Flyway : spring. Flyway.locations 被忽略

java - 仅显示 Spring 启动验证的默认消息 - MethodArgumentNotValidException

java - 如何使用 Java 在特定时间触发一个方法?

java - 使用Java Rest高级客户端的Elasticsearch Node IP地址和其他详细信息

java - Solr.XML 中的未知标签核心

java - JSF FacesTagExtraInfo 类不在 JSF 1.2 v 中

Spring 验证@AssertTrue

java - 服务器启动时测试数据库可用性

java - 如何在 Spring 中使用 Maxlength <Form :Input> tag?

java - Spring Boot @RestController 使用属性启用/禁用方法