haroba
haroba

Reputation: 2310

Spring Boot using properties file of parent module instead of its own

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

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

<context:property-placeholder location="submodule.properties" />

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?

Edit: 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?

Upvotes: 2

Views: 8354

Answers (1)

Plog
Plog

Reputation: 9622

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.

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")

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.

Upvotes: 3

Related Questions