2

I am loading the custom configuration from application.yml in my spring boot service.

I have annotated by bean class as below,

    @Component
    @ConfigurationProperties("app")
    public class ContinentConfig {

    private Map<String, List<Country>> continents = new HashMap<String, List<Country>>();

          //get/set/tostring methods
  }

My custom class Country includes 2 fields,

public class Country {

    String name;
    String capital;

    //get/set/tostring methods
}

In application.yml I have as below,

app:
  continents: 
    Europe: 
      - name: France
        capital: Paris
    Asia: 
      - name: China
        capital: Beijing       

With the above setup, I am able to load the config from application.yml.

I now want to extract the config to a separate continentconfig.yml in the same src/main/resources folder. So, I moved the custom config to continentconfig.yml leaving other properties like server.port in application.yml.

The continentconfig.yml has the same content as I had earlier in application.yml.

I also added the below annotations to the ContinentConfig Class,

@Component
@ConfigurationProperties("app")
@EnableConfigurationProperties
@PropertySource(value="classpath:continentconfig.yml")
public class ContinentConfig {

}

After this change, I see the config is not getting loaded from continentconfig.yml to ContinentConfig bean.

Can someone please help in resolving the issue.

Anthon
  • 69,918
  • 32
  • 186
  • 246
juser
  • 359
  • 1
  • 7
  • 17
  • just take a look at this answer here:[https://stackoverflow.com/questions/21271468/spring-propertysource-using-yaml/54247009#54247009](https://stackoverflow.com/questions/21271468/spring-propertysource-using-yaml/54247009#54247009) .it`s easy to use. – Forest10 Jan 18 '19 at 04:13

2 Answers2

1

The short answer you can't do that and you should use the property file.

24.6.4 YAML shortcomings

YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file.

You can create your initializer and use the YamlPropertySourceLoader.

Anton N
  • 2,317
  • 24
  • 28
  • Thanks for clarifying. But, is there another way to externalize the custom config in it own yml file , perhaps using it as a profile specific yml file. – juser Jun 14 '17 at 10:52
  • You can have a base configuration in `application.yml` and you can override it for required profiles `application-dev.yml` etc. There is another workaround by using `YamlPropertySourceLoader`. See https://stackoverflow.com/questions/21271468/spring-propertysource-using-yaml – Anton N Jun 14 '17 at 18:09
0

I believe by externalizing you mean using a property file to load the configuration from a file configured in github/or other such hosting repos? You could very well do that by using bootstrap.yml. This loads all the configurations from an external file and also allows the provision to override them using the local application.properties/application.yml

spring: application: name: cloud : config : uri :

Also make sure that you have spring cloud in your pom to resolve this just in case

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>

Just incase if your local yml property files are not loaded in your classpath, then add the following

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.jks</include>
                </includes>
            </resource>
        </resources>

Note: It is preferred to have the YamlPropertySourceLoader to load your config file to your classpath and on top of that you could use the above config

vijayakumarpsg587
  • 1,079
  • 3
  • 22
  • 39