简体   繁体   English

Spring 应用程序的库模块如何向应用程序的“application.yml”添加额外的配置?

[英]How can a library module of a Spring application add additional configuration to the application's `application.yml`?

I have a Spring Boot application built with Maven which uses JDBC.我有一个使用 Maven 构建的 Spring 引导应用程序,它使用 JDBC。 The application.yml file has application.yml文件有

spring:
  application:
    (stuff)
  datasource:
    url: jdbc:informix-sqli://......
    driver-class-name: com.informix.jdbc.IfxDriver

I want to move the JDBC specific parts into a library so now the app/src/main/resources/application.yml only contains我想将 JDBC 特定部分移动到库中,所以现在app/src/main/resources/application.yml仅包含

spring:
  application:
    (stuff)

and the datasource configuration parameters need to live in the library repository.并且datasource配置参数需要存在于库存储库中。 I tried creating lib/src/main/resources/application.yml with我尝试使用创建lib/src/main/resources/application.yml

spring:
  datasource:
    url: jdbc:informix-sqli://......
    driver-class-name: com.informix.jdbc.IfxDriver

hoping that both the yml files would be picked up and merged when Spring loads up.希望当 Spring 加载时,两个 yml 文件都会被拾取并合并。 Apparently not.显然不是。

The library and application build fine, but when I run it库和应用程序构建良好,但是当我运行它时

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

How can I get the configuration in the library to be merged into the application.yml configuration in the application?如何将库中的配置合并到应用程序中的application.yml配置中?

I have always found "merging" of external configuration from sub-modules with spring-boot to be problematic at worst, confusing at best.我一直发现子模块的外部配置与 spring-boot 的“合并”在最坏的情况下是有问题的,在最好的情况下是令人困惑的。 I now organize all external configuration using one of two approaches.我现在使用两种方法之一来组织所有外部配置。 But first, there are multiple ways to specify external configuration that you should be aware of:但首先,有多种方法可以指定您应该注意的外部配置:

I previously would specify multiple locations and that sort-of worked.我以前会指定多个位置并且那种工作。 But, I now typically use one of two approaches to avoid confusion:但是,我现在通常使用以下两种方法之一来避免混淆:

  • I use profiles and specify multiple profiles at runtime when launching the spring boot app.在启动 spring 启动应用程序时,我使用配置文件并在运行时指定多个配置文件。 ie Multiple profiles like "shared-common, shared-jdbc, deploy-prod" which will load "application-shared-jdbc.yml" out the sub-module.即多个配置文件,如“shared-common、shared-jdbc、deploy-prod”,它将从子模块加载“application-shared-jdbc.yml”。

or或者

  • I create a module that contains nothing but configuration files that get used by all related modules, often with multiple profiles for different configuration scenarios.我创建了一个模块,它只包含所有相关模块使用的配置文件,通常具有用于不同配置场景的多个配置文件。 All other modules (executable and libraries) depend on this shared configuration module.所有其他模块(可执行文件和库)都依赖于这个共享配置模块。

AFAICT, spring-boot's external configuration handling not setup ideally for having standalone submodule configuration. AFAICT,spring-boot 的外部配置处理没有理想地设置为具有独立的子模块配置。 It's more oriented around the notion that configuration belongs to runtime/executable modules, not libraries.它更倾向于配置属于运行时/可执行模块,而不是库的概念。

you can use @PropertySource.你可以使用@PropertySource。 you have to implement your own PropertySourceFactory if your props is in yaml format.如果你的道具是 yaml 格式,你必须实现你自己的 PropertySourceFactory。 then define it in the PropertySource然后在 PropertySource 中定义它

@PropertySource(value = ResourceUtils.CLASSPATH_URL_PREFIX
    + "application.yml", factory = Factory.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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