如何像普通 Spring 属性一样访问 Spring Boot 的构建属性?

问题描述 投票:0回答:2

我想将

/META-INF/build-info.properties
中的构建属性用作普通 Spring 属性,这样我就可以在 Logback 配置中的 via
spring-property
中使用它们
logback-spring.xml

我已经能够使用它们了,但是有些事情发生了变化......

  • 属性文件
    /META-INF/build-info.properties
    在那里。
  • 我能够将 bean
    BuildProperties
    作为具有正确值的 bean 获取。
  • 构建属性未在应用程序上下文中注册为属性源?

有人知道如何像我的

logback-spring.xml
中的普通 Spring 属性一样使用构建属性吗?

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

  <springProperty scope="context" name="application" source="build.name"/>
  <springProperty scope="context" name="version" source="build.version"/>
java spring-boot logback
2个回答
2
投票

在 Spring Boot 应用程序中,确保 build-info.properties 文件作为属性源加载。这可以通过将以下条目添加到您的 application.properties 来完成

spring.config.name=application,build-info
spring.config.location=classpath:/,classpath:/META-INF/

将构建属性作为普通 Spring 属性加载后,您可以通过使用 ${propertyKey} 语法引用它们来在 logback-spring.xml 配置文件中使用它们。

就像例子-:

<configuration>
   <property resource="META-INF/build-info.properties" />

   <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
       <!-- Use the build property in the appender configuration -->
       <encoder pattern="${build.version} - %msg%n" />
   </appender>

   <root level="INFO">
       <appender-ref ref="CONSOLE" />
   </root>
</configuration>

在此示例中,从 build-info.properties 文件访问 ${build.version} 属性并在配置中使用。


0
投票

旧线程,但我只想留下答案以供记录。在 application.properties 中配置

spring.config.location
spring.config.name
can 不起作用。我的意思是...想想看...无论如何:解决方案是在
spring.config.import
中提供额外的文件,如下所示:

spring.config.import=classpath:git.properties,classpath:META-INF/build-info.properties

此处有完整说明:https://docs.spring.io/spring-boot/docs/2.4.2/reference/htmlsingle/#boot-features-external-config-files-importing

© www.soinside.com 2019 - 2024. All rights reserved.