Привести пользовательский файл свойств к объекту сложного типа в Java - PullRequest
0 голосов
/ 29 марта 2019

Я создаю пользовательский файл свойств в 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);
            }

1 Ответ

0 голосов
/ 29 марта 2019

Вы можете использовать @ConfigurationProperties из SpringBoot.

Подробнее об этом здесь: https://www.baeldung.com/configuration-properties-in-spring-boot

...