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

Nim*_*sky 5 java spring properties

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

@Value("${myproperties.myValue}")
private String myString
Run Code Online (Sandbox Code Playgroud)

但是,我现在想要遍历文件中的所有属性,当它们的名称未知时,并存储值和名称.spring和java的最佳方法是什么?

Evg*_*eev 33

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

public class Test1 {
    @Autowired
    Properties props;

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

...

<util:properties id="props" location="/spring.properties" />
Run Code Online (Sandbox Code Playgroud)