1

There is a Spring Boot 2 app with such a structure:

parent-module
    module-1
        src
            main
                java
                resources
                - application.yml
    module-2
        src
            main
                java
                resources
                - application.yml

Also, module-1 depends on module-2, specified in pom.xml dependencies section.

The problem is that when I specify some properties in module-2's application.yml - they are not visible in main module-1's components (via @Value annotation).

As was answered here seems like module-1's application.yml overrides module-2's application.yml. There is a workaround - if I use name application.yaml in module-2 everything works fine, but I'm going to add more modules and, finally, it's dirty hack.

What I'm doing wrong? Should such an hierarchy of property files specified somehow? I will be happy to provide more details if it's needed.

Thank you!

Dmitry Adonin
  • 1,064
  • 3
  • 16
  • 36

3 Answers3

2

Spring Boot is a runtime framework. I understand that your modules are not spring-boot applications by themselves (you can't make a dependency on a spring boot application packaged with spring boot maven plugin, because it produces an artifact that is not really a JAR from the Java's standpoint although it does have *.jar extension).

If so, they're probably regular jars. So you should have a "special" module that assembles the application. This special module lists both 'module1' and 'module2' in <dependency> section and should contain a definition of spring-boot-maven-plugin in its build section (assuming you're using maven). But if so you shouldn't really have more than one application.yml - it will be misleading. Instead, put the application.yml to the src/main/resources of that "special" module.

If you really have to for whatever reason work with multiple application.yaml files, make sure you've read this thread

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
1

I know, this is already a well-aged post.

I just came accross the same issue and the best solution I found was to import the module-specific configurations with the spring.config.import directive as described here.

In this case you still have your module specific configuration in property or yaml files within that specific module and do not have too much unwanted dependencies in your project setup.

downdrown
  • 341
  • 1
  • 3
  • 14
0

application.yml is, as the name indicates, an application-level file, not a module-level file.

It is the build script that assembles the final application, e.g. the .war file, that needs to include a application.yml file, if any.

If modules need properties, and cannot rely on the defaults, e.g. using the : syntax in @Value("${prop.name:default}"), they need to provide a module-level property file using @PropertySource("classpath:/path/to/module-2.properties").

Note: By default, @PropertySource doesn't load YAML files (see official documentation), but Spring Boot can be enhanced to support it. See @PropertySource with YAML Files in Spring Boot | Bealdung.


Alternative: Have the application-level build script (the one building the .war file) merge multiple module-level build scripts into a unified application.yml file.

Andreas
  • 154,647
  • 11
  • 152
  • 247