如何在spring-boot中添加多个application.properties文件? [英] How to add multiple application.properties files in spring-boot?

查看:493
本文介绍了如何在spring-boot中添加多个application.properties文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Spring boot application that is divided in several modules. The main module runs the application and has an application.properties file 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).

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

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

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

推荐答案

使用Maven或Gradle时,您尝试做的事情将无法正常工作。原因是当创建工件(很可能是因为你使用Spring Boot之后),根目录中只会有一个 application.properties 文件。

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.properties file in the root.

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

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 this or this answer for pointers), or use some kind of merging task for your build tool (something like this perhaps)

这篇关于如何在spring-boot中添加多个application.properties文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆