[英]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:但首先,有多种方法可以指定您应该注意的外部配置:
Spring Boot and multiple external configuration files Spring 引导和多个外部配置文件
Starting Spring Application by merging yml files 通过合并 yml 文件启动 Spring 应用程序
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
I previously would specify multiple locations and that sort-of worked.我以前会指定多个位置并且那种工作。 But, I now typically use one of two approaches to avoid confusion:但是,我现在通常使用以下两种方法之一来避免混淆:
or或者
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.