1

I have a Spring Boot application built with Maven which uses JDBC. The application.yml file has

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

spring:
  application:
    (stuff)

and the datasource configuration parameters need to live in the library repository. I tried creating lib/src/main/resources/application.yml with

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. 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?

spraff
  • 32,570
  • 22
  • 121
  • 229

2 Answers2

2

I have always found "merging" of external configuration from sub-modules with spring-boot to be problematic at worst, confusing at best. 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. i.e. Multiple profiles like "shared-common, shared-jdbc, deploy-prod" which will load "application-shared-jdbc.yml" out the sub-module.

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. It's more oriented around the notion that configuration belongs to runtime/executable modules, not libraries.

kaliatech
  • 17,579
  • 5
  • 72
  • 84
0

you can use @PropertySource. you have to implement your own PropertySourceFactory if your props is in yaml format. then define it in the PropertySource

@PropertySource(value = ResourceUtils.CLASSPATH_URL_PREFIX
    + "application.yml", factory = Factory.class)
Ryan Guamos
  • 240
  • 4
  • 14
  • a) where in the library source do I put the `@PropertySource`? b) there is no `@PropertySource` in the existing application source code, or the pom as far as I can see, so what are the rules for that? c) the existing configuration lives in a yml, why do I need to use `@PropertySourceFactory` if the application is already using yml? – spraff Apr 29 '20 at 17:23
  • just create a class in the library and annotate it with @Configuration together with the `@PropertySource`. you don't need `@PropertySourceFactory`. take a look at this [link](https://medium.com/@james.tran/how-to-read-profile-based-yaml-configurations-with-propertysource-8131c16b3fc6) – Ryan Guamos Apr 29 '20 at 17:35
  • That link uses `@PropertySourceFactory`, what you you mean I don't need it? – spraff Apr 29 '20 at 17:53
  • did you really read it? you don't need the annotation `@PropertySourceFactory` what you need is an implementation of the the interface `PropertySourceFactory` you need to implement that interface just like the one in the link that I gave you. Spring does not support reading YAML file using `@PropertySource` that is why there is a `factory` attribute in the `@PropertySource`. try converting your YAML props to normal properties file so you can understand what I'm saying. – Ryan Guamos Apr 29 '20 at 17:58
  • Try again [Config](https://pastebin.com/sd3cEfkC) & [Implementation](https://pastebin.com/hZX7BZDy) – Ryan Guamos Apr 29 '20 at 18:19
  • don't forget to import the Config class in your main app put this `@Import(Config.class)` together with the `@SpringBootApplication` – Ryan Guamos Apr 29 '20 at 18:27