3

I currently have 4 YAML files

application.yaml
application-local.yaml
application-test.yaml
application-dev.yaml

I assume that application.yaml is acting as default.

In the other 3 files contains

spring:
  profiles:
    include: default

My problem is that my application.yaml is very large and it contains properties that are hierarchical.

For example,

connection:
  baseUrl: 'http://localhost:5000'
  brokerUrl: 'http://localhost:8082'
  ...

workspaces:
  a:
    width: 45
    height: 100
    ...
  b:
    width: 56
    height: 125
    ...

I would like to keep the content in current application.yaml in multiple files in subfolders.

Let's say connection.yaml will contain

connection:
  baseUrl: 'http://localhost:5000'
  brokerUrl: 'http://localhost:8082'
  ...

workspace-a.yaml will contain

workspaces:
  a:
    width: 45
    height: 100
    ...

and lastly, workspace-b.yaml will contain

workspaces:
  a:
    width: 56
    height: 125
    ...

at the end the structure will be like

/resources
  application.yaml
  application-local.yaml
  application-test.yaml
  application-dev.yaml
  connection.yaml
  /workspaces
    a.yaml
    b.yaml

I've tried to put the following code in application.yaml but it didn't work.

spring:
  profiles:
    active: 'local'
    include:
      - connection.yaml
      - workspaces/a.yaml
      - workspaces/b.yaml

I also have tried different formats like

- classpath:/workspaces/a.yaml
- ./workspaces/a.yaml
- file:./workspaces/a.yaml

Please help :'(

  • You can use the @PropertySource annotation for this. [Check this thread please](https://stackoverflow.com/questions/23134869/spring-boot-how-to-use-multiple-yml-files) – tudvari May 16 '20 at 07:41
  • Thank you so much but the file is very large and would be splitted into many small files, I'm not sure if @PropertySource will help in this case. I kinda want a clean and nice code. Do you have any other alternatives? – clumsy.kitten May 16 '20 at 07:45

3 Answers3

2

Since Spring-Boot 2.4 you can use spring.config.import to import into your config.

So you could use something like this in your application.yml

spring.config.import: connection.yml

Spring Blog post here

athom
  • 1,428
  • 4
  • 13
  • 26
1

You could try naming your subconfigs like this application-a.yaml,application-b.yaml and then use it in application.yaml

spring:
   profiles:
      include:
         a,
         b,
         ...

The idea taken from: https://stackoverflow.com/a/40500683/10938777

martaisty
  • 33
  • 6
  • 1
    The problem is I have to keep `application-a.yaml` and `application-b.yaml` under the same folder as the `application.yaml`. I want to keep them in a sub-folder, but thank you so much :D – clumsy.kitten May 16 '20 at 10:28
0

Unfortunately I don't have better solution for you. You can use multiple source file this annotation: PropertySources annotation

This annotation can bind multiple PropertySource into one configuration.

For example:

@PropertySources({ @PropertySource("/file1.properties"), @PropertySource("/file2.properties") })

tudvari
  • 26
  • 2
  • `@PropertySources`, unfortunately, doesn't work with yaml files, but I'm trying to find the workaround here https://stackoverflow.com/questions/21271468/spring-propertysource-using-yaml – clumsy.kitten May 18 '20 at 08:28