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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…