[英]Reading from yaml is showing null values
To read from yaml file i am doing the following 要从yaml文件中读取,我正在执行以下操作
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("partners")
public class YAMLConfig {
private Map<String,String> partners = new HashMap<>();
public void setPartners(Map<String, String> partners) {
this.partners = partners;
}
public Map<String, String> getPartners() {
return partners;
}
}
yaml file yaml文件
person2:
name: bbb
addresses:
to: jiang
bit: su
partners:
p1: wallet
p2: wallet
p3: wallet
And in other Java file i am Autowiring to get YamlConfig above 在其他Java文件中,我自动装配以获得YamlConfig
@Service
public class ObjectModificationService {
@Autowired
YAMLConfig yamlConfig;
public JSONObject modify(JSONObject jsonObject ) {
String type = yamlConfig.getPartners().get(partnerName.toLowerCase());
}
}
I am not getting any type above , also checked yamlConfig.getPartners() in debug it is coming as null. 我没有上面的任何类型,也检查调试中的yamlConfig.getPartners()它是否为空。 I was following this tutorial: https://www.baeldung.com/spring-yaml , but then again IDE is showing error when i put ConfigurationProperties without ConfigurationProperties("partners").
我正在关注本教程: https : //www.baeldung.com/spring-yaml ,但是当我将ConfigurationProperties放在没有ConfigurationProperties(“合作伙伴”)的情况下IDE再次显示错误。
In your yml configuration class, you have the properties annotation set to "partners", this means that the YAMLConfig class will attempt to read the partners section only. 在yml配置类中,将属性注释设置为“partners”,这意味着YAMLConfig类将仅尝试读取partner部分。 The "person2" section will not be read.
“person2”部分将不会被阅读。
To retrieve the dependency injected values, simply call: yamlConfig.getPartners()
要检索依赖注入值,只需调用:
yamlConfig.getPartners()
Also, be sure that the application.yml is in the resources directory and/or in the same directory as the .jar 此外,请确保application.yml位于资源目录中和/或与.jar位于同一目录中
you have specified Partners map accepts String values. 您指定的合作伙伴地图接受字符串值。 so, try Map value formatted with quotes ('') in yaml config file
所以,在yaml配置文件中尝试使用引号('')格式化的Map值
person2:
name: bbb
addresses:
to: jiang
bit: su
partners:
p1: 'wallet'
p2: 'wallet'
p3: 'wallet'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.