Spring 弹簧载荷yaml列表值
是否有任何简单的方法可以从Spring 弹簧载荷yaml列表值,spring,spring-boot,Spring,Spring Boot,是否有任何简单的方法可以从yaml文件加载特定对象的列表。例如,我得到了配置如下的yaml文件: list: - name: a url: a.com - name: b url: b.com 我想从这个属性创建列表。我知道使用spring引导和@ConfigurationProperties注释很容易做到,但如何使用spring呢 这个怎么样 yml文件 list: 'a,a.com;b,b.com' 组件类 @Value("#{T(or
yaml
文件加载特定对象的列表。例如,我得到了配置如下的yaml
文件:
list:
-
name: a
url: a.com
-
name: b
url: b.com
我想从这个属性创建列表
。我知道使用spring引导和@ConfigurationProperties
注释很容易做到,但如何使用spring呢 这个怎么样
yml文件
list: 'a,a.com;b,b.com'
组件类
@Value("#{T(org.blah.spring.service.Endpoint).getEndpoints('${list}'.split(';'))}")
List<Endpoint> endpoints;
@Value(“#{T(org.blah.spring.service.Endpoint).getEndpoints('${list}.split(';'))}”)
列出终点;
和端点
@Getter
@Setter
@AllArgsConstructor
public class Endpoint {
private String name;
private String url;
public static List<Endpoint> getEndpoints(List<String> strings){
List<Endpoint> endpoints = Lists.newArrayList();
for(String s: strings){
String split[] = s.split(",");
endpoints.add(new Endpoint(split[0], split[1]));
}
return endpoints;
}
}
@Getter
@塞特
@AllArgsConstructor
公共类终结点{
私有字符串名称;
私有字符串url;
公共静态列表getEndpoints(列表字符串){
List endpoints=Lists.newArrayList();
用于(字符串s:字符串){
字符串拆分[]=s.split(“,”);
添加(新端点(拆分[0],拆分[1]);
}
返回端点;
}
}
这个怎么样
yml文件
list: 'a,a.com;b,b.com'
组件类
@Value("#{T(org.blah.spring.service.Endpoint).getEndpoints('${list}'.split(';'))}")
List<Endpoint> endpoints;
@Value(“#{T(org.blah.spring.service.Endpoint).getEndpoints('${list}.split(';'))}”)
列出终点;
和端点
@Getter
@Setter
@AllArgsConstructor
public class Endpoint {
private String name;
private String url;
public static List<Endpoint> getEndpoints(List<String> strings){
List<Endpoint> endpoints = Lists.newArrayList();
for(String s: strings){
String split[] = s.split(",");
endpoints.add(new Endpoint(split[0], split[1]));
}
return endpoints;
}
}
@Getter
@塞特
@AllArgsConstructor
公共类终结点{
私有字符串名称;
私有字符串url;
公共静态列表getEndpoints(列表字符串){
List endpoints=Lists.newArrayList();
用于(字符串s:字符串){
字符串拆分[]=s.split(“,”);
添加(新端点(拆分[0],拆分[1]);
}
返回端点;
}
}
如果您正在寻找无引导解决方案(如果您正在构建库),请忽略我的答案
使用属性配置工厂
,YamlPropertySourceLoader
和可变属性资源
可以将Yaml
文件读入Pojo:
import org.junit.Test;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.ByteArrayResource;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class YamlTest {
private static final String YAML_STRING = "list: \n" +
" - \n" +
" name: a \n" +
" url: a.com \n" +
" - \n" +
" name: b \n" +
" url: b.com";
@Test
public void shouldLoadYamlIntoObject() throws Exception {
PropertiesConfigurationFactory<EndpointsHolder> propertiesConfigurationFactory = new PropertiesConfigurationFactory<>(EndpointsHolder.class);
MutablePropertySources propertySources = new MutablePropertySources();
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
propertySources.addFirst(yamlPropertySourceLoader.load("list", new ByteArrayResource(YAML_STRING.getBytes()), null));
propertiesConfigurationFactory.setPropertySources(propertySources);
EndpointsHolder actual = propertiesConfigurationFactory.getObject();
assertTrue(actual.getList().get(0).getName().equals("a"));
assertTrue(actual.getList().get(1).getUrl().equals("b.com"));
}
public static class EndpointsHolder {
List<Endpoints> list;
public List<Endpoints> getList() {
return list;
}
public void setList(List<Endpoints> list) {
this.list = list;
}
}
public static class Endpoints {
String name;
String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
import org.junit.Test;
导入org.springframework.boot.bind.propertiesConfiguration工厂;
导入org.springframework.boot.env.YamlPropertySourceLoader;
导入org.springframework.core.env.MutablePropertySources;
导入org.springframework.core.io.ByteArrayResource;
导入java.util.List;
导入静态org.junit.Assert.assertTrue;
公共类YamlTest{
私有静态最终字符串YAML\u String=“列表:\n”+
“-\n”+
“名称:a\n”+
“url:a.com\n”+
“-\n”+
“名称:b\n”+
“网址:b.com”;
@试验
public void shouldlLoadYamlInToObject()引发异常{
PropertiesConfiguration工厂PropertiesConfiguration工厂=新的PropertiesConfiguration工厂(EndpointsHolder.class);
MutablePropertySources propertySources=新的MutablePropertySources();
YamlPropertySourceLoader YamlPropertySourceLoader=新YamlPropertySourceLoader();
propertySources.addFirst(yamlPropertySourceLoader.load(“list”,新的ByteArrayResource(YAML_STRING.getBytes()),null));
PropertiesConfiguration工厂。设置propertySources(propertySources);
EndpointsHolder actual=propertiesConfigurationFactory.getObject();
assertTrue(实际的.getList().get(0.getName().equals(“a”));
assertTrue(actual.getList().get(1.getUrl().equals(“b.com”));
}
公共静态类EndpointsHolder{
名单;
公共列表getList(){
退货清单;
}
公共无效集合列表(列表){
this.list=列表;
}
}
公共静态类终结点{
字符串名;
字符串url;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getUrl(){
返回url;
}
公共void setUrl(字符串url){
this.url=url;
}
}
}
只要用您自己的数据源替换新的ByteArrayResource(YAML_STRING.getBytes())。如果您正在寻找无引导解决方案(如果您正在构建库),请忽略我的回答 使用
属性配置工厂
,YamlPropertySourceLoader
和可变属性资源
可以将Yaml
文件读入Pojo:
import org.junit.Test;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.ByteArrayResource;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class YamlTest {
private static final String YAML_STRING = "list: \n" +
" - \n" +
" name: a \n" +
" url: a.com \n" +
" - \n" +
" name: b \n" +
" url: b.com";
@Test
public void shouldLoadYamlIntoObject() throws Exception {
PropertiesConfigurationFactory<EndpointsHolder> propertiesConfigurationFactory = new PropertiesConfigurationFactory<>(EndpointsHolder.class);
MutablePropertySources propertySources = new MutablePropertySources();
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
propertySources.addFirst(yamlPropertySourceLoader.load("list", new ByteArrayResource(YAML_STRING.getBytes()), null));
propertiesConfigurationFactory.setPropertySources(propertySources);
EndpointsHolder actual = propertiesConfigurationFactory.getObject();
assertTrue(actual.getList().get(0).getName().equals("a"));
assertTrue(actual.getList().get(1).getUrl().equals("b.com"));
}
public static class EndpointsHolder {
List<Endpoints> list;
public List<Endpoints> getList() {
return list;
}
public void setList(List<Endpoints> list) {
this.list = list;
}
}
public static class Endpoints {
String name;
String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
import org.junit.Test;
导入org.springframework.boot.bind.propertiesConfiguration工厂;
导入org.springframework.boot.env.YamlPropertySourceLoader;
导入org.springframework.core.env.MutablePropertySources;
导入org.springframework.core.io.ByteArrayResource;
导入java.util.List;
导入静态org.junit.Assert.assertTrue;
公共类YamlTest{
私有静态最终字符串YAML\u String=“列表:\n”+
“-\n”+
“名称:a\n”+
“url:a.com\n”+
“-\n”+
“名称:b\n”+
“网址:b.com”;
@试验
public void shouldlLoadYamlInToObject()引发异常{
PropertiesConfiguration工厂PropertiesConfiguration工厂=新的PropertiesConfiguration工厂(EndpointsHolder.class);
MutablePropertySources propertySources=新的MutablePropertySources();
YamlPropertySourceLoader YamlPropertySourceLoader=新YamlPropertySourceLoader();
propertySources.addFirst(yamlPropertySourceLoader.load(“list”,新的ByteArrayResource(YAML_STRING.getBytes()),null));
PropertiesConfiguration工厂。设置propertySources(propertySources);
EndpointsHolder actual=PropertiesConfiguration工厂.getObject