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



我在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);
            }

你可以使用SpringBoot中的@ConfigurationProperties

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

最新更新