首页 > 解决方案 > 将自定义属性文件转换为java中的复杂类型对象

问题描述

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

//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);
            }

标签: javaspring-bootproperties-file

解决方案


您可以@ConfigurationProperties从 SpringBoot 使用。

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


推荐阅读