48

In Spring Boot, I know that I can replace application.properties with application.yml and use the YAML format. However, my application.yml is getting crowded so I need to split it up a bit. How can I do that? I would like to do something like this:

...
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@EnableScheduling
@PropertySource({"classpath:application.yml", "classpath:scheduling.yml"})
public class ApplicationConfig {
...
Johan Frick
  • 1,084
  • 1
  • 10
  • 18
  • Maybe this approach may work: http://stackoverflow.com/questions/28303758/how-to-use-yamlpropertiesfactorybean-to-load-yaml-files-using-spring-framework-4 – Håvard Semundseth Jan 07 '16 at 08:23

5 Answers5

58
  1. remove @PropertySource annotation, you not need it
  2. rename your scheduling.yml into src/main/resources/application-scheduling.yml
  3. add in src/main/resources/application.yml file next line:

    spring.profiles.include: 'scheduling'

Maksim Kostromin
  • 3,273
  • 1
  • 32
  • 30
  • 2
    I think the OP wants to have properties grouped by different yaml files. Does your solution solve that issue? – bluelurker Jul 13 '20 at 14:46
19

if I have a lot of configurations and/or environments, usually I do so:

$ cat src/main/resources/application.yml:
spring:
  profiles:
    include: >
      profile1,
      profile2,
      ...
      profileN

$ ls -lah src/main/resources/config:
drwxr-xr-x  4 mak  staff   136B Apr 16 23:58 .
drwxr-xr-x  6 mak  staff   204B Apr 17 01:54 ..
-rw-r--r--  1 mak  staff    60B Apr 16 23:58 application-profile1.yml
-rw-r--r--  1 mak  staff    62B Apr 16 23:16 application-profile2.yml
...
-rw-r--r--  1 mak  staff    50B Apr 16 23:16 application-profileN.yml
Maksim Kostromin
  • 3,273
  • 1
  • 32
  • 30
17

@PropertySource does not support YAML (probably it will in Spring 4.1). You can set spring.config.location or spring.config.name to a comma-separated list (e.g. as System property or command line argument).

Personally I like all my YAML in the same place (the structure really helps to break it up visually, and you can use documents inside the file to split it up more). That's just taste I guess.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Thanks, I'll keep it in one file for now then. It would be nicer to split it up though, but I will wait for Spring 4.1. – Johan Frick Apr 22 '14 at 19:59
  • For integration testing, if you want to having application.yml file in test/resources and if you want to override only few attributes other than /java/resources how can you do that – plzdontkillme Dec 11 '14 at 16:58
  • 1
    Use `@ActiveProfiles` and only put the overrides in the profile you use for testing. Or use `@IntegrationTest`. Or `ReflectionTestUtils`. – Dave Syer Dec 12 '14 at 07:27
  • 1
    It's nice to put everything in the same place until it becomes too large to maintain, in which case breaking things up like the questioner asks is ideal. – Hazok Apr 05 '16 at 19:27
  • The feature request to add support to YAML files to `@PropertySource` was never implemented, and has been closed: https://github.com/spring-projects/spring-framework/issues/18486. The comments by the Spring developers indicate pushback on adding it, since users were wanting to use it in a Spring Boot context where it was not a proper fix. – M. Justin Dec 10 '20 at 21:34
11

Suppose your application need 4 .yml files.

application.yml
application-dev.yml
application-uat.yml
application-prod.yml

and you have to set different setting for each file.

You just need to set your setting on appropriate environment such as dev, uat ot prod level and the have to add just one property in application.yml file.

  spring:  
    profiles:
       active: dev
    application: /* optional */
       name: Application Name 
Deva
  • 1,851
  • 21
  • 22
9

You can use active profile concept in your main yaml file. For example:

spring.profiles.active: test

that means you should have application-test.yml file in your resource directory. Consider that active profiles will override properties with the same names in your main yaml file.

For more information visit: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

Jalal Sajadi
  • 412
  • 6
  • 12