英文:
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-:
<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>
In this example, the ${build.version} property is accessed from the build-info.properties file and used within the <encoder> configuration.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论