简体   繁体   中英

How to create per module application.properties in spring boot?

I have a spring boot project in which there are multiple modules, I want to have each module separate application properties file, but when I added properties file in all modules, it's still picking properties from main application properties file.

Project Structure:

|-- Dockerfile
|-- build.gradle
|-- modules
|   |-- application
|   |   |-- build.gradle
|   |   `-- src
|   |       `-- main
|   |           |-- java
|   |           |   `-- org
|   |           |       `-- example
|   |           |           |-- CoreApplication.java
|   |           `-- resources
|   |               |-- application-beta.properties
|   |               |-- application-dev.properties
|   |               |-- application-local.properties
|   |               |-- application-prod.properties
|   |               |-- application-test.properties
|   |               `-- application.properties
|   |-- config-management
|   |   |-- build.gradle
|   |   `-- src
|   |       `-- main
|   |           |-- java
|   |           |   `-- org
|   |           |       `-- example
|   |           |           `-- controller
|   |           |               `-- TestController.java
|   |           `-- resources
|   |               |-- application-beta.properties
|   |               |-- application-dev.properties
|   |               |-- application-local.properties
|   |               |-- application-prod.properties
|   |               |-- application-test.properties
|   |               `-- application.properties
`-- settings.gradle

application.properties in config module

config.hello=hello-from-config

application.properties in application module

config.hello=hello-from-application

TestController.java in config module

@RestController
public class TestController {
    @Value("${config.hello}")
    private String hello;

    @GetMapping("hello")
    public String get() {
        return hello;
    }
}

After calling /hello api, response: hello-from-application

This isn't a complete answer to your question but more a list of advices I think you should follow when designing your multi-module application project.

  • With spring many things are easier to configure with @Configuration beans. On many occasions, you'll be forced to have such beans to configure specific functionalities. These configuration beans are also a great way of including a configuration of one module in another.
  • What I do is the following - each module has one main @Configuration bean. This bean @Import s all other @Configuration beans inside your module, as well as, loads the properties file and imports the main @Configuration beans of the modules and maybe external dependencies your module depends on.
  • You should remember that spring, and java in general uses the classloader to load these .properties files that you have in the resources folders. The way you have organized the .property files currently they override each other; depending on how spring boot works the classloader will either load the first *.properties file it encounters, or maybe, your extra property files won't even make it into the shaded jar file. As a solution, use packages. Each module should have all of its stuff in a specific package, this includes the configuration files.
  • Do not include environment-related configuration files in your application source code. Move it to your deployment procedures. You can have samples of these environment-related files in your /docs or /distribution folder in your project, but have them here as documentation and not as actual configurations used in these environments. In 99% of the cases, environment-related configurations should be stored separately from your compiled application, the way you have it now they are compiled into the application itself.

Hope this was helpful.

PS Once you organize the config files in the manner I describe, you may still end up fighting a battle against Spring's property file loader(don't remember the exact name), but that's a separate topic, and there are already many questions on that topic here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM