Java 如何在 spring-boot 中添加多个 application.properties 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33229793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:29:14  来源:igfitidea点击:

How to add multiple application.properties files in spring-boot?

javaspringspring-boot

提问by mat_boy

I have a Spring boot application that is divided in several modules. The main module runs the application and has an application.propertiesfile in the resources folder. I'm wondering if I can add another properties file in a submodule to separate properties that are belonging to that module and how to make this working (because it is not).

我有一个分为几个模块的 Spring 启动应用程序。主模块运行应用程序并application.properties在资源文件夹中有一个文件。我想知道是否可以在子模块中添加另一个属性文件来分隔属于该模块的属性以及如何使其工作(因为它不是)。

+main_module
  +src
    +main
      +java
        +my/package/Application.java
        +resources/application.properties
+support_module
  +src
    +main
      +java
        +resources/application.properties

So, this is the current situation. Clearly the properties file in the module support_moduleis not read causing a NoSuchBeanDefinitionException, while if I put the content in the other properties file everything works fine.

所以,这就是目前的情况。显然,模块中的属性文件support_module未被读取,导致NoSuchBeanDefinitionException,而如果我将内容放在其他属性文件中,则一切正常。

采纳答案by geoand

What you are trying to do will not work when using Maven or Gradle. The reason is that when the artifact (jar most likely since you are using Spring Boot) is created, there will only be one application.propertiesfile in the root.

使用 Maven 或 Gradle 时,您尝试执行的操作将不起作用。原因是在创建工件(最有可能是 jar,因为您使用的是 Spring Boot)application.properties时,根目录中将只有一个文件。

I suggest you either change the name of the properties file on the support module and then configure Spring Boot to look for that file as well (take a look at thisor thisanswer for pointers), or use some kind of merging task for your build tool (something like thisperhaps)

我建议您更改支持模块上的属性文件的名称,然后配置 Spring Boot 以查找该文件(查看答案以获得指针),或者为您的构建使用某种合并任务工具(像这样也许)

回答by srsajid

You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths)

您还可以使用 spring.config.location 环境属性(以逗号分隔的目录位置列表或文件路径)引用显式位置

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

For more information clickhere

欲了解更多信息,请单击此处

回答by zeronone

Spring Boot reads the property files in the following order. (From Spring Boot in Action)

Spring Boot 按以下顺序读取属性文件。(来自 Spring Boot 实战

  1. Externally, in a /config subdirectory of the directory from which the application is run
  2. Externally, in the directory from which the application is run
  3. Internally, in a package named “config”
  4. Internally, at the root of the classpath

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

  1. 在外部,在运行应用程序的目录的 /config 子目录中
  2. 在外部,在运行应用程序的目录中
  3. 在内部,在名为“config”的包中
  4. 在内部,在类路径的根部

该列表按优先级排序(在列表中较高位置定义的属性覆盖在较低位置定义的属性)。

So placing application.propertiesin a configsub-directory will give it a higher priority. In the following configuration, the application.propertiesfrom module_awill take precedence. You can add common defaults in application.propertiesand override them in individual modules by placing the configuration file in config/application.properties.

因此,放置application.propertiesconfig子目录中将赋予它更高的优先级。在以下配置中,application.propertiesfrommodule_a将优先。您可以application.properties通过将配置文件放在config/application.properties.

+common_module
  +src
    +main
      +java
      +resources/application.properties
+module_a
  +src
    +main
      +java
        +my/package/Application.java
      +resources/config/application.properties

回答by Imtiaz Shakil Siddique

The problem is exactly what @geoand describes. Spring boot loads top level application.propertiesand ignores any properties file with the exact name located in other jars.

问题正是@geoand 所描述的。Spring boot 加载顶级application.properties并忽略位于其他 jars 中的具有确切名称的任何属性文件。

But I didn't find any concrete implementation on how to fix this problem, so here it is for those who wants to know the implementation.

但是我没有找到关于如何解决这个问题的任何具体实现,所以这里是为那些想知道实现的人提供的。

Consider this project configuration:

考虑这个项目配置:

+main_module
  +src
    +main
      +java
        +my/package/Application.java
      +resources/application.properties
+module_aa
  +src
    +main
      +java
        +my/package/config/ModuleAAConfig.java
      +resources/module_aa.properties
+module_bb
  +src
    +main
      +java
        +my/package/config/ModuleBBConfig.java
      +resources/module_bb.properties

Now to load properties for each sub modules correctly we need to add @PropertySourceannotation on the configs of each module i.e ModuleAAConfig.java, ModuleBBConfig.java.

现在要正确加载每个子模块的属性,我们需要@PropertySource在每个模块的配置上添加注释,即ModuleAAConfig.java, ModuleBBConfig.java.

Example:

例子:

ModuleAAConfig.java

ModuleAAConfig.java

package my.package.config;

@Configuration
@PropertySource(
    ignoreResourceNotFound = false,
    value = "classpath:module_aa.properties")
public class ModuleAAConfig {}

ModuleBBConfig.java

ModuleBBConfig.java

package my.package.config;

@Configuration
@PropertySource(
    ignoreResourceNotFound = false,
    value = "classpath:module_bb.properties")
public class ModuleBBConfig {}


Bonus:

奖金:

If you want to load profile specific property, you can do so by utilizing spring variables e.g.

如果你想加载配置文件特定的属性,你可以通过使用 spring 变量来实现,例如

@PropertySource("classpath:module_aa-${spring.profiles.active}.properties")