使用@Value 在 Spring Boot 2.0 中访问 buildInfo

access buildInfo in Spring Boot 2.0 with @Value

之前在 Spring 引导 1.x 中,我编写了一个 Gradle 任务来将 jar 的构建版本复制到应用程序的 application.yml 并替换给定的 属性 通过正则表达式,例如info.build.version: 0.0.1

迁移到 Spring Boot 2.0,我意识到 io.spring.dependency-management 插件允许我定义 buildInfo 任务:

springBoot {
    buildInfo()
}

访问 /info 执行器时,这有效并成功显示相同的信息。

现在我想在两个用例中使用 META-INF/build-info.properties 中生成的 build.version

  1. 在 SwaggerUI 中显示版本
  2. 在每个日志行中包含版本

以前,像这样访问 属性 就足够了:@Value("${info.build.version:undefined}") String buildVersion 或者在 logback-spring.xml 中:

<springProperty scope="context" name="applicationVersion" source="info.build.version"/>

不幸的是,即使我将 info.build.version 替换为 build.version ,这两个访问器都不再工作(正如我所期望的那样)。

我相信将版本包含在 logback 中距离通过 @Value 注释访问 属性 仅一小步,所以这是我问题的核心:

如何通过@Value访问META-INF/build-info.properties中生成的build.version

我也试过添加任务

processResources {
    filesMatching('build-info.properties') {
        expand(project.properties)
    }    
}

中所建议,但这似乎没有任何效果。

您需要将文件 META-INF/build-info.properties 作为 @PropertSource 添加到您的 Spring-Boot-Application:

@SpringBootApplication()
@PropertySource("classpath:META-INF/build-info.properties")
public class MyApp implements WebMvcConfigurer { 
  ...
}

之后,您可以在 Controller/Service/...

中通过 @Value 访问 属性 build.version
@Controller(...)
public MyController {
    @Value("${build.version}") String myVersion;
    ...
}

当谈到 spring-boot 时,spring 处理的几乎所有信息很可能都可以作为 spring 托管 bean 访问。

关于构建信息,spring-boot 公开了一个名为 buildProperties 的自动装配的 bean。这是通过 ProjectInfoAutoConfiguration 模块发生的。

连同 Spring 表达式语言 (SpEL) 对 @Value 注释的支持,您可以获得如下所述的预期结果。

@Value("#{buildProperties.get('version')}")           // not 'build.version'
private String myAppBuildVersion;

或者更好的是,将 buildProperties bean 直接自动装配到您的组件,以便您可以随心所欲地使用它。

@Autowired
private BuildProperties buildProperties;

NOTE: The autoconfiguration strips off the build. prefix. So your SpEL expressions should use version as key. Not build.version.


更新:

我首先对你的问题感到困惑,因为问题更多的是关于如何使用 @Value 注释。所以我将保留以上答案。

为了帮助您使用 logback-spring.xml,将 build-info.properties 导入 logback-spring.xml,如下所示。这允许使用 logback place-holders 访问 build-info.properties 中的每个键。 (不要将此与 SPRING 属性 占位符或 SPRING-表达式混淆)

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
  <springProperty scope="context" name="appLogTarget" source="app.log.target"
    defaultValue="CONSOLE"/>
  <property resource="META-INF/build-info.properties" />


  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>[${build.version}] %d{ISO8601}" %-5p [%c{3}"] \(%t:%X{}"\) %m%n</Pattern>
    </layout>
  </appender>
 <root>
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </root>
</xml>

<properties/> 标签的 "resource" 属性 将查看 classpath resources 并找到合适的标签。

Spring 2.4 开始,您可以使用 spring.config.import 属性。例如,在 application.yml:

spring.config.import: "classpath:META-INF/build-info.properties"
...
sentry.version: ${build.version}

同样 @Value("${build.version}") 也可以,或者访问 logback-spring.xml 中的 属性。