在多模块Spring Boot应用中覆盖的application.yml

huangapple go评论75阅读模式
英文:

Overriden application.yml in multi-module Spring Boot app

问题

以下是翻译好的内容:

有一个结构如下的Spring Boot 2应用程序:

父模块
    模块-1
        src
            main
                java
                resources
                - application.yml
    模块-2
        src
            main
                java
                resources
                - application.yml

此外,模块-1依赖于模块-2,这在pom.xml的dependencies部分中指定。

问题是,当我在模块-2的application.yml中指定一些属性时,它们在主模块-1的组件中(通过@Value注解)不可见。

就像这里所回答的那样,似乎模块-1的application.yml会覆盖模块-2的application.yml。有一个解决方法 - 如果我在模块-2中使用名称application.yaml,一切都能正常工作,但我将要添加更多模块,最终这是一个不太规范的方法。

我做错了什么?这样的属性文件层次结构是否需要以某种方式指定?如果需要的话,我可以提供更多细节。

谢谢!

英文:

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!

答案1

得分: 2

Spring Boot 是一个运行时框架。我理解你的模块本身不是 Spring Boot 应用程序(你不能将依赖放在使用 Spring Boot Maven 插件打包的 Spring Boot 应用程序上,因为它生成的是一个从 Java 角度来看不真正是 JAR 的构件,尽管它的文件扩展名是 *.jar)。

如果是这样的话,它们可能是普通的 JAR 包。所以你应该有一个“特殊”的模块来组装应用程序。这个特殊的模块在 <dependency> 部分列出了 module1module2,并且在其 build 部分应该包含 spring-boot-maven-plugin 的定义(假设你在使用 Maven)。但是如果是这样的话,你实际上不应该有多个 application.yml 文件 - 这会产生误导。相反,将 application.yml 放在那个“特殊”模块的 src/main/resources 目录下。

如果由于某种原因你确实必须使用多个 application.yaml 文件,请确保你已经阅读了这个讨论

英文:

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 &lt;dependency&gt; 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

答案2

得分: 1

我知道,这篇帖子已经存在一段时间了。

我刚刚遇到了相同的问题,我找到的最佳解决方案是使用 spring.config.import 指令导入特定模块的配置,如这里所描述的那样。

在这种情况下,您仍然可以在特定模块内的属性或yaml文件中拥有您的模块特定配置,并且在项目设置中不会有太多不需要的依赖。

英文:

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.

答案3

得分: 0

application.yml 是如其名称所示,一个应用级别的文件,而不是模块级别的文件。

它是组装最终应用程序(例如 .war 文件)的构建脚本,如果需要包含 application.yml 文件的话,就需要在其中提供一个 application.yml 文件。

如果模块需要属性,并且不能依赖默认值,例如使用 @Value("${prop.name:default}") 中的 : 语法,它们需要提供一个模块级别的属性文件,使用 @PropertySource("classpath:/path/to/module-2.properties")

注意: 默认情况下,@PropertySource 不加载 YAML 文件(请参阅官方文档),但是Spring Boot可以增强以支持它。请参阅Spring Boot中的YAML文件与@PropertySource | Bealdung

备选方案: 让应用级别的构建脚本(构建 .war 文件的脚本)合并多个模块级别的构建脚本到一个统一的 application.yml 文件中。

英文:

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(&quot;${prop.name:default}&quot;), they need to provide a module-level property file using @PropertySource(&quot;classpath:/path/to/module-2.properties&quot;).

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.

huangapple
  • 本文由 发表于 2020年9月11日 19:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63846115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定