application.yml
countries:
country:
- name: kenya
insuffbal: test101
inactive: test101
- name: botswana
insuffbal: test102
inactive: test101
上面的yml结构需要使用Bean映射到java对象。
Countries.java
@Component
@ConfigurationProperties(prefix = "countries.ke")
public class Countries {
//Need to map here - help me here
}
Application.java
@SpringBootApplication
@EnableConfigurationProperties(Countries.class)
public class Application {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
RepoProperties repoProperties = context.getBean(RepoProperties.class);
repoProperties.print();
}
}
答案 0 :(得分:0)
使用http://www.jsonschema2pojo.org/
只需将yaml
文件粘贴到那里,然后选择来源类型yaml
这是您需要定义的bean文件
@Configuration
@ConfigurationProperties(prefix = "countries")
public class Countries {
private List<Country> country = null;
public List<Country> getCountry() {
return country;
}
public void setCountry(List<Country> country) {
this.country = country;
}
public static class Country {
private String name;
private String insuffbal;
private String inactive;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInsuffbal() {
return insuffbal;
}
public void setInsuffbal(String insuffbal) {
this.insuffbal = insuffbal;
}
public String getInactive() {
return inactive;
}
public void setInactive(String inactive) {
this.inactive = inactive;
}
}
}
现在只需在您想要使用的地方自动装配Countries
。