We design our Spring Boot application modules to be loosely coupled, but sometimes we need access to application.properties files in other modules. To set the stage, here we see a JPA Module application.properties file and other modules that require access to it.
Access Example One
We talked about a “Master Switch” in our NixMash Blogging app in a previous post determining the app's current Private/Public runtime mode. A “Master Switch” is in one place only, otherwise we'd have to call it something else. That “switch” is in the form of an application.properties setting in our JPA Module, a dependency to all other modules. Its property name is nixmash.mode.enabled. We sometimes override it at the command line, so it's an integral property of our application across all modules.
For example, our MAIL Module will use that property to set a local boolean variable for determining which Freemarker template directory to use. We'll access our JPA application.properties file with an @Import of the JPA ApplicationConfig.
Our JPA ApplicationConfig class loads our all-important application.properties file.
Multi-Module Spring Boot Property File Names Tip
This is a good time to recommend using .properties file names unique to each module if you're going to do much cross-module property access. Okay, moving on…
We want our application configuration to be as flexible as possible, so for External Property files we want developers to be able to set the locations and file names for their own environment in the module's .properties file. Here is an example of SolrSettings in the SOLR Module.
Our MVC Module enjoys Solr Search capabilities from our SOLR Module and so will need access to the SOLR Module's application.properties file. We'll again use an @Import, this time in our MVC WebConfig.
When All Else Fails…
When all else fails and you need access to another Module's .properties file you can set the path. Here we do so in a SOLR Module Test Configuration class because the location won't be changing and we're testing.
Source Code Notes for this Post
Source code discussed in this post can be found in my NixMash Blog project located on GitHub.