Java 使用@Value/SpEL通过构造函数构建对象列表
我有以下资料: 应用程序属性Java 使用@Value/SpEL通过构造函数构建对象列表,java,spring,spring-el,Java,Spring,Spring El,我有以下资料: 应用程序属性 indexes=1,2;2,3;3,4 我想将这些属性绑定到列表,但无法找到正确的SpEL语法 Index.java public class Index { public Index(int x, int y) { this.x = x; this.y = y; } } ... List<Index> indexList; public Service(@Value(value = "#{'${i
indexes=1,2;2,3;3,4
我想将这些属性绑定到列表
,但无法找到正确的SpEL语法
Index.java
public class Index {
public Index(int x, int y) {
this.x = x;
this.y = y;
}
}
...
List<Index> indexList;
public Service(@Value(value = "#{'${indexes}'.split(';')}")
List<String> properties) {
List<Index> result = new ArrayList<>();
for (String s : properties) {
String[] split = s.split(",");
Index index = new Index(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
result.add(index);
}
this.indexList = result;
}
现在,我正在用分号分隔符拆分构造函数中的值,然后构建一个列表
Service.java
public class Index {
public Index(int x, int y) {
this.x = x;
this.y = y;
}
}
...
List<Index> indexList;
public Service(@Value(value = "#{'${indexes}'.split(';')}")
List<String> properties) {
List<Index> result = new ArrayList<>();
for (String s : properties) {
String[] split = s.split(",");
Index index = new Index(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
result.add(index);
}
this.indexList = result;
}
。。。
列表索引列表;
公共服务(@Value(Value=“#{${index}.split(“;”)}”)
列表属性){
列表结果=新建ArrayList();
用于(字符串s:属性){
字符串[]split=s.split(“,”);
索引索引=新索引(Integer.valueOf(split[0]),Integer.valueOf(split[1]);
结果:添加(索引);
}
this.indexList=结果;
}
我能用一行吗?比如:
@Value(value = "some_spring_magic")
List<Index> indexList;
@Value(Value=“some\u spring\u magic”)
列表索引列表;
替换代码>与,
类似:
@Value(value = "#{'${indexes}'.replaceAll(';',',').split(',')}")
package foo;
import java.util.stream.Stream;
public class Splitter {
public static String[] split(String str, String delimeters){
Stream<String> s = Stream.of(str);
for (int i = 0; i < delimeters.length(); i++) {
String delimiter = delimeters.substring(i, i+1);
s = s.flatMap(r -> Stream.of(r.split(delimiter)));
}
return s.toArray(String[]::new);
}
}
另一个想法是使用拆分器实用程序,如下所示:
@Value(value = "#{'${indexes}'.replaceAll(';',',').split(',')}")
package foo;
import java.util.stream.Stream;
public class Splitter {
public static String[] split(String str, String delimeters){
Stream<String> s = Stream.of(str);
for (int i = 0; i < delimeters.length(); i++) {
String delimiter = delimeters.substring(i, i+1);
s = s.flatMap(r -> Stream.of(r.split(delimiter)));
}
return s.toArray(String[]::new);
}
}
package-foo;
导入java.util.stream.stream;
公共类拆分器{
公共静态字符串[]拆分(字符串str、字符串delimeters){
流s=流of(str);
对于(int i=0;iStream.of(r.split(delimiter));
}
返回s.toArray(字符串[]::new);
}
}
然后:
@Value(value = "#{T(foo.Splitter).split('${indexes}', ';,')}")
List<String> properties;
@Value(Value=“#{T(foo.Splitter).split(“${index},”;,“)}”)
列出财产;
我认为没有必要在SpEL中构建复杂的表达式。您只能将值和进程作为bean传递到预期列表中。例如:
@配置
公共类AppConfig{
@豆子
列表索引列表(@Value(${index}”)字符串索引列表){
返回Arrays.asList(indexList.split(“;”))
.stream().map(s->{
字符串[]split=s.split(“,”);
返回新索引(Integer.valueOf(split[0]),Integer.valueOf(split[1]);
}).collect(Collectors.toList());
}
以后可以按如下方式使用:
@Autowired
列表索引列表;
?