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

问题描述 投票:0回答:1

我在java 1.8中构建了一个自定义属性文件,我可以读它谎言属性但是我想要做的是我可以用complextype对象获取这个属性文件。我不想用键和值读取我想直接将它转换为对象,我怎样才能做到这一点

//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);
            }
java spring-boot properties-file
1个回答
0
投票

你可以使用Spring Boot的@ConfigurationProperties

在这里阅读更多相关信息:https://www.baeldung.com/configuration-properties-in-spring-boot

© www.soinside.com 2019 - 2024. All rights reserved.