如何将Spring Boot的构建属性作为普通的Spring属性访问?

huangapple go评论66阅读模式
英文:

How to access Spring Boot`s build properties as normal Spring properties?

问题

我想将/META-INF/build-info.properties中的构建属性用作普通的Spring属性,以便我可以通过logback-spring.xml中的spring-property来使用它们。

我之前已经能够使用它们,但是有些东西发生了变化...

  • /META-INF/build-info.properties属性文件位于那里。
  • 我可以获取到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"/>
英文:

I would like to use the build properties in /META-INF/build-info.properties as normal Spring properties, so that I am able to use them in via spring-property in my Logback configuration logback-spring.xml.

I was already able to use them, but something changed...

  • The properties file /META-INF/build-info.properties is there.
  • I am able get the bean BuildProperties as bean with the correct values.
  • The build properties are not registred in the application context as property sources?

Does someone knows, how to use the build properties like normal Spring properties in my logback-spring.xml?

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

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

答案1

得分: 2

在您的Spring Boot应用程序中,请确保build-info.properties文件被加载为属性源。可以通过向application.properties添加以下条目来实现:

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

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

例如:

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

   <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
       <!-- 在appender配置中使用构建属性 -->
       <encoder pattern="${build.version} - %msg%n" />
   </appender>

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

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

英文:

In your Spring Boot application, make sure that the build-info.properties file is being loaded as a property source. This can be done by adding the following entry to your application.properties

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

Once the build properties are loaded as normal Spring properties, you can use them in your logback-spring.xml configuration file by referencing them using the ${propertyKey} syntax.

like example-:

&lt;configuration&gt;
   &lt;property resource=&quot;META-INF/build-info.properties&quot; /&gt;

   &lt;appender name=&quot;CONSOLE&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
       &lt;!-- Use the build property in the appender configuration --&gt;
       &lt;encoder pattern=&quot;${build.version} - %msg%n&quot; /&gt;
   &lt;/appender&gt;

   &lt;root level=&quot;INFO&quot;&gt;
       &lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
   &lt;/root&gt;
&lt;/configuration&gt;

In this example, the ${build.version} property is accessed from the build-info.properties file and used within the <encoder> configuration.

huangapple
  • 本文由 发表于 2023年7月13日 14:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定