将自定义属性文件转换为Java中的复杂类型对象

时间:2019-03-29 09:24:04

标签: java spring-boot properties-file

我在Java 1.8中构建了一个自定义属性文件,我可以读取它的lie属性,但是我想要做的是可以使用complextype object获得此属性文件。我不想使用键和值进行读取,但我想将其转换直接指向对象,该怎么办

//Connection properties is my custom properties file class
      prop = new Properties();
            String filename = "connection.properties";
            input = ConnectionProperties.class.getClassLoader().getResourceAsStream(filename);
            if(input==null){
                System.out.println("Sorry, unable to find " + filename);
                return;
            }

            //load a properties file from class path, inside static method
            prop.load(input);

            Enumeration<?> e = prop.propertyNames();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = prop.getProperty(key);
                System.out.println("Key : " + key + ", Value : " + value);
            }

1 个答案:

答案 0 :(得分:0)

您可以使用SpringBoot中的@ConfigurationProperties

在此处了解更多信息:https://www.baeldung.com/configuration-properties-in-spring-boot

相关问题