gpt4 book ai didi

spring-boot - 覆盖作为 jar 一部分的 application.properties 中的 spring 属性

转载 作者:行者123 更新时间:2023-12-05 07:47:39 25 4
gpt4 key购买 nike

我正在开发一个 spring boot 应用程序(我们称之为 MyLib)。它使用 Spring 云流。这个想法是这个应用程序将被另一个 java 应用程序用作 jar(我们称之为 MyApp。它可能不是 spring boot)。我想要做的是 MyApp 将指定 spring.cloud.stream.bindings.<channel>.destination MyLib 中的代码将使用它。

这是可以实现的吗?

最佳答案

从外部 jar 加载 application.properties

我不知道 spring.cloud.stream.bindings 特别是,但是一个 spring Boot 应用程序可以从多个加载它的 application-${profile}.properties位置,包括类路径上的 jar :

Spring Boot documentation :

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

  • A /config subdirectory of the current directory.
  • The current directory
  • A classpath /config package
  • The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

...

The default search path classpath:,classpath:/config,file:,file:config/ is always used, irrespective of the value of spring.config.location

换句话说,application-${profile}.properties 文件必须位于 ././config/MyApp.jar

您还可以使用 spring.config.location 定义其他查找文件夹,但这必须在运行时完成:

spring.config.name and spring.config.location are used very early to determine which files have to be loaded so they have to be defined as an environment property (typically OS env, system property or command line argument).


查找顺序

你提到:

Overriding spring properties in application.properties

考虑属性的顺序如下 ( from spring documentation ):

  • @TestPropertySource annotations on your tests.
  • Command line arguments.
  • Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
  • ServletConfig init parameters.
  • ServletContext init parameters.
  • JNDI attributes from java:comp/env.
  • Java System properties (System.getProperties()).
  • OS environment variables.
  • A RandomValuePropertySource that only has properties in random.*.
  • Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
  • Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
  • Application properties outside of your packaged jar (application.properties and YAML variants).
  • Application properties packaged inside your jar (application.properties and YAML variants).
  • @PropertySource annotations on your @Configuration classes.
  • Default properties (specified using SpringApplication.setDefaultProperties).

关于spring-boot - 覆盖作为 jar 一部分的 application.properties 中的 spring 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39479817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com