Spring boot 如何根据输入从yml获取值?
我使用的是spring boot starter web最新版本2.2.6.0。我需要根据输入从yml文件中获取值,而不是@value 如果计数为100,则需要获取以下值Spring boot 如何根据输入从yml获取值?,spring-boot,yaml,app.yaml,Spring Boot,Yaml,App.yaml,我使用的是spring boot starter web最新版本2.2.6.0。我需要根据输入从yml文件中获取值,而不是@value 如果计数为100,则需要获取以下值 key1: value1 100 key2: value2 100 key1: value1 1000 key2: value2 1000 如果计数为1000,则需要获取以下值 key1: value1 100 key2: value2 100 key1: value1 1000 key2: value2 1000
key1: value1 100
key2: value2 100
key1: value1 1000
key2: value2 1000
如果计数为1000,则需要获取以下值
key1: value1 100
key2: value2 100
key1: value1 1000
key2: value2 1000
我怎样才能做到这一点
My application.yml文件
config:
host: http://myhost
count-100:
key1: value1 100
key2: value2 100
count-1000:
key1: value1 1000
key2: value2 1000
count-10000:
key1: value1 10000
key2: value2 10000
Java代码
int count = myObject.getCount();
if (count >= 100) {
// this needs to fill from application.yml
key1 = "";
key2 = 0;
} else if (count >=1000 && count <= 10000) {
key1 = "";
key2 = 0;
} else {
key1 = "";
key2 = 0;
}
int count=myObject.getCount();
如果(计数>=100){
//这需要从application.yml中填写
键1=“”;
键2=0;
}否则,如果(计数>=1000&&count使用@ConfigurationProperties
加载计数值
我建议您将应用程序yml更改为使用计数作为键,后跟不同的计数
差不多
config:
host: http://myhost
counts:
100:
key1: value1 100
key2: value2 100
1000:
key1: value1 1000
key2: value2 1000
10000:
key1: value1 10000
key2: value2 10000
创建一个计数
类
@Configuration
@ConfigurationProperties("config")
public class Counts {
private final Map<Integer, Map<String, String>> counts;
public Counts(Map<Integer, Map<String, String>> counts) {
this.counts = counts;
}
public Map<Integer, Map<String, String>> getCounts() {
return counts;
}
}
@配置
@配置属性(“配置”)
公共类计数{
私人最终地图计数;
公众计数(地图计数){
这个。计数=计数;
}
公共映射getCounts(){
返回计数;
}
}
Java代码
//Autowire Counts class
int count = myObject.getCount();
Map<Integer, Map<String, String>> countMap = counts.getCounts().get(count);
key1 = countMap.get("key1");
key2 = countMap.get("key2");
if (count >= 100) {
} ....
//Autowire Counts class
int count = myObject.getCount();
if (count >= 100) {
Map<String, String> count100Map = counts.getCount100();
key1 = count100Map.get("key1");
key2 = count100Map.get("key2");;
} ....
//自动导线计数类
int count=myObject.getCount();
Map countMap=counts.getCounts().get(count);
key1=countMap.get(“key1”);
key2=countMap.get(“key2”);
如果(计数>=100){
} ....
如果您想保留您的应用程序yml,您可以使用
@Configuration
@ConfigurationProperties("config")
public class Counts {
private final Map<String, String> count100;
private final Map<String, String> count1000;
private final Map<String, String> count10000;
public Counts(Map<String, String> count100, Map<String, String> count1000, Map<String, String> count10000) {
this.count100 = count100;
this.count1000 = count1000;
this.count10000 = count10000;
}
public Map<String, String> getCount1000() {
return count1000;
}
public Map<String, String> getCount100() {
return count100;
}
public Map<String, String> getCount10000() {
return count10000;
}
}
@配置
@配置属性(“配置”)
公共类计数{
私人最终地图计数100;
私人最终地图计数1000;
私人最终地图计数10000;
公共计数(地图计数100、地图计数1000、地图计数10000){
this.count100=count100;
this.count1000=count1000;
this.count10000=count10000;
}
公共地图getCount1000(){
返回计数1000;
}
公共地图getCount100(){
返回计数100;
}
公共地图getCount10000(){
返回计数10000;
}
}
Java代码
//Autowire Counts class
int count = myObject.getCount();
Map<Integer, Map<String, String>> countMap = counts.getCounts().get(count);
key1 = countMap.get("key1");
key2 = countMap.get("key2");
if (count >= 100) {
} ....
//Autowire Counts class
int count = myObject.getCount();
if (count >= 100) {
Map<String, String> count100Map = counts.getCount100();
key1 = count100Map.get("key1");
key2 = count100Map.get("key2");;
} ....
//自动导线计数类
int count=myObject.getCount();
如果(计数>=100){
Map count100Map=counts.getCount100();
key1=count100Map.get(“key1”);
key2=count100Map.get(“key2”);;
} ....
从何处获得计数为100或1000?您能显示代码吗?我已更新了代码。您能查看我的答案吗?谢谢您的答案。我正在检查此问题,但我的springboot应用程序没有从应用程序中选取值。yml..一旦我完成此操作..我将重新投票给您的答案。