gpt4 book ai didi

java - 使用 spring 和 java 遍历文件中的所有属性

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:09 24 4
gpt4 key购买 nike

通常,当我知道属性名称时,我会使用注释填充字段:

@Value("${myproperties.myValue}")
private String myString

但是我现在想遍历文件中的所有属性,当它们的名称未知时,将值和名称都存储在那里。使用 spring 和 java 的最佳方式是什么?

最佳答案

其实如果你只需要从文件中读取属性,而不需要在Spring的属性占位符中使用这些属性,那么解决方案就很简单

public class Test1 {
@Autowired
Properties props;

public void printProps() {
for(Entry<Object, Object> e : props.entrySet()) {
System.out.println(e);
}
}

...

<util:properties id="props" location="/spring.properties" />

关于java - 使用 spring 和 java 遍历文件中的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14135655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com