12

I'm using Spring boot for my project and trying to load yaml files so that I can use the data in the files in my project. For example I have content in my application.yml like below.

currency:
     code:
        840: 2
        484: 2
        999: 0

And in my code for reading the content from application.yml I have a class like this.

import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "currency")
public class Currency {

   private Map<String, String> code;

   public Map<String, String> getCode() {
       return code;
   }
   public void setCode(Map<String, String> code) {
       this.code = code;
   }
}

And If I print it in the test class

public class Test{

@Autowired
Currency currency;

Map<String, String> test = currency.getCode();
       for (Map.Entry<String, String> entry : test.entrySet()) {
           System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
       }
}

I'm getting like below which is perfect.

Key : 840 Value : 2
Key : 484 Value : 2
Key : 999 Value : 0

This is working if I keep the application.yml in my jar itself or I can read it by placing it in git repo aswell.

I tried keeping the content in currency.yml and in my application.yml I tried to use spring.config.location, so that I can read the content from currency.yml directly but it didn't work.

I would like to load files like currency.yml and codes.yml etc... which are custom yml files so that I can read multiple files content and use in my application. Are there any annotations or some approach I can use to load custom.yml files?

Arun
  • 2,312
  • 5
  • 24
  • 33
  • 1
    Did you look into http://stackoverflow.com/questions/28303758/how-to-use-yamlpropertiesfactorybean-to-load-yaml-files-using-spring-framework-4? Here yaml file is being loaded via simple PropertyPlaceholderConfigurer and its specific configuration. – tkachuko Jun 22 '16 at 18:03
  • @tkachuko No. Will try that. Thanks. – Arun Jun 22 '16 at 18:28
  • @Arun did you try it? – Adrian Ivan Aug 16 '16 at 13:24
  • @AdrianIvan Hey, no I didn't but I will need to do it when I get a chance. – Arun Aug 16 '16 at 16:16

2 Answers2

21

In the .yml file of the project add below content.

spring:
  profiles:
    include:
      - currency

Note: You can refer to multiple .yml files this way if you want. You need additional .yml files and Config classes like below example.

You need to have another .yml file which is application-currency.yml

In application-currency.yml you can add currencies, like below

currencies:
  mappings:
    USD:
      fraction: 2
      symbol: $
      text: "US Dollar"
    MXN:
      fraction: 2
      symbol: $
      text: "Mexican Peso"

Your Configuration class would look like below.

package com.configuration;

import com.Currency;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "currencies")
public class CurrencyConfiguration {

    private Map<String, Currency> mappings;

    public Map<String, Currency> getMappings() {
        return mappings;
    }

    public void setMappings(Map<String, Currency> mappings) {
        this.mappings = mappings;
    }
}

Wherever you need to use the currency details you can get by calling as shown below.

@Autowired
private CurrencyConfiguration currencyConfiguration;

String currencyCodeAlpha3 = "USD";
Currency currency = currencyConfiguration.getMappings().get(currencyCodeAlpha3);
Arun
  • 2,312
  • 5
  • 24
  • 33
  • 1
    While this certainly works, it feels like an abuse of Spring profiles. Spring profiles are designed to support different (loosely-defined) "environments", with different values set in different environments: [Bean Definition Profiles ](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-definition-profiles). The list of currencies isn't conceptually an environment, but rather something that should always be present. – M. Justin Dec 10 '20 at 21:45
3

You can use spring.config.location to specify paths to additional config files as a comma separated list.

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Magnus
  • 7,952
  • 2
  • 26
  • 52