如何根据输入从yml中获取值?
How to get values from yml based on input?
我正在使用 spring-boot-starter-web 最新版本 2.2.6.RELEASE。我需要根据我的输入而不是@value 从 yml 文件中获取值。
如果计数为100,需要得到以下值
key1: value1 100
key2: value2 100
如果计数为1000,需要得到以下值
key1: value1 1000
key2: value2 1000
我怎样才能做到这一点?
我的 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;
}
非常感谢此处的任何输入。
使用@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
创建 Counts
class
@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;
}
}
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) {
} ....
如果您想保留您的应用程序 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;
}
}
Java代码
//Autowire Counts class
int count = myObject.getCount();
if (count >= 100) {
Map<String, String> count100Map = counts.getCount100();
key1 = count100Map.get("key1");
key2 = count100Map.get("key2");;
} ....
我正在使用 spring-boot-starter-web 最新版本 2.2.6.RELEASE。我需要根据我的输入而不是@value 从 yml 文件中获取值。
如果计数为100,需要得到以下值
key1: value1 100
key2: value2 100
如果计数为1000,需要得到以下值
key1: value1 1000
key2: value2 1000
我怎样才能做到这一点?
我的 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;
}
非常感谢此处的任何输入。
使用@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
创建 Counts
class
@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;
}
}
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) {
} ....
如果您想保留您的应用程序 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;
}
}
Java代码
//Autowire Counts class
int count = myObject.getCount();
if (count >= 100) {
Map<String, String> count100Map = counts.getCount100();
key1 = count100Map.get("key1");
key2 = count100Map.get("key2");;
} ....