Spring Boot 使用父模块的属性文件而不是它自己的 [英] Spring Boot using properties file of parent module instead of its own

查看:32
本文介绍了Spring Boot 使用父模块的属性文件而不是它自己的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Java 项目具有以下结构:

My Java project has the following structure:

module/
    pom.xml
    config/
        application.properties
    src/main/java/
        spring boot application
    submodule/
        pom.xml
        src/
            main/
                java/
                    spring boot application
                resources/
                    submodule.properties

父模块中的 Spring Boot 应用程序应从 config/application.properties 读取其属性,而子模块中的 Spring Boot 应用程序应使用 submodule/src/main/resources/submodule.properties.

The Spring Boot app in the parent module should read its properties from config/application.properties, while the Spring Boot app in the submodule should use submodule/src/main/resources/submodule.properties.

然而,不管我做什么,子模块中的应用程序继续使用父模块的属性文件.尤其是

However, regardless of what I do, the app in the submodule continues to use the parent module's properties file. In particular,

@PropertySource("classpath:submodule.properties")

没有任何影响.添加也没有

doesn't have any effect. Neither does adding

到子模块的应用程序上下文.关于如何让子模块中的应用程序只使用它自己的属性文件而不是它的父级文件的任何建议?

to the submodule's application context. Any suggestions as to how I can make the app in the submodule use only its own properties file and not its parent's?

15 分钟后,我意识到确实使用了子模块的属性文件,但无论出于何种原因,外部 config/application.properties 文件都有 优先于内部模块自己的属性文件.

Fifteen minutes later, I realized that the submodule's properties file is indeed used, but for whatever reason the outer config/application.properties file has priority over the inner module's own properties file.

这种行为对我来说毫无意义,因为人们会认为模块内部的属性会更特定于该模块,因此让内部属性文件覆盖外部文件会更有意义.

This behaviour makes no sense to me, since one would think the properties inside the module would be more specific to that module, and thus letting the inner properties file override the outer file would make more sense.

有谁知道关闭这种行为的方法,并让内部属性文件覆盖外部属性文件?

Does anyone know of a way to turn off this behaviour, and let the inner properties file override the outer one?

推荐答案

Spring Boot 会自动查找 application.properties 文件并使这些属性可用.由于这些属性将在父项目中首先加载,因此它们的属性将优先于您子模块中的任何属性.

Spring Boot automatically looks for an application.properties file and makes these properties available. Since these properties will be loaded first as they are in the parent project their properties will take priority over any properties in your submodule.

如果您不希望子模块加载 application.properties,那么我建议重命名它,以便 Spring Boot 不会自动获取它,然后您可以通过以下方式在任何需要的地方自行注册:

If you don't want the submodule to load application.properties then I would recommend renaming it so that it isn't automatically picked up by Spring Boot and then you can register it yourself wherever it is needed by doing:

@PropertySource("classpath:renamed-application.properties")

如果您的意图是仅覆盖 application.properties 中的某些属性,而仍然使用其余属性,那么这显然行不通.但实际上父 application.properties 中的所有属性应该对所有子模块都是通用的.如果您要覆盖该行为,那么实际上您应该为那些特定的子模块使用不同的属性名称,因为它特定于该模块.

If your intention was to only override some of the properties in application.properties and still use the rest then this obviously won't work. But really all properties in the parent application.properties should be common to all submodules. If you are overriding that behaviour then really you should just use different property names for those specific sub modules since it is specific for that module.

这篇关于Spring Boot 使用父模块的属性文件而不是它自己的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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