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

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

问题描述

我有一个分为几个模块的 Spring 启动应用程序.主模块运行应用程序并在资源文件夹中有一个 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 时,您尝试执行的操作将不起作用.原因是在创建工件(最有可能是 jar,因为您使用的是 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 以查找该文件(查看 thisthis 答案以供参考),或为您的构建工具使用某种合并任务(例如 this 也许)

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天全站免登陆