Это мой класс:NumberFormatException при введении Int свойства
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
PropertyPlaceholderConfigurer pph = new PropertyPlaceholderConfigurer();
pph.setLocations(new Resource[]{new ClassPathResource("one.properties"), new ClassPathResource("two.properties")});
context.addBeanFactoryPostProcessor(pph);
context.refresh();
Controller obj1 = (Controller) context.getBean("controller");
System.out.println(obj1.getMessage());
Controller2 obj2 = (Controller2) context.getBean("controller2");
System.out.println(obj2.getMessage());
System.out.println(obj2.getInteger());
Это соответствующая конфигурация XML:
<bean id="controller" class="com.sample.controller.Controller">
<property name="message" value="${ONE_MESSAGE}"/>
</bean>
<bean id="controller2" class="com.sample.controller.Controller2">
<property name="message" value="${TWO_MESSAGE}"/>
<property name="integer" value="${TWO_INTEGER}"/>
</bean>
one.properties:
ONE_MESSAGE=ONE
two.properties:
TWO_MESSAGE=TWO
TWO_INTEGER=30
TWO_MESSAGE назначается правильно как String TWO. Я получаю NumberFormatException при введении TWO_INTEGER. Есть ли способ достичь этого, не добавляя сеттер, который берет String и скрывает его до int в классе Controller2?
Ошибка:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'controller2' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'integer'; nested exception is java.lang.NumberFormatException: For input string: "${TWO_INTEGER}"
Спасибо.
«PropertyPlaceholderConfigurer», очевидно, не находит файлы свойств или файлы свойств не содержат никаких свойств, таких как '$ {TWO_INTEGER}'. Вы проверяете это? – LaurentG
Я проверил это. На самом деле, другие свойства String из того же файла получают правильно. one.properties имеет: 'ONE_MESSAGE = ONE' и two.properties имеет:' TWO_MESSAGE = TWO Свойства TWO_INTEGER = 30' и 'TWO_MESSAGE' читаются правильно. Просто для уточнения. есть две строки в двух. Свойства не так, как он отформатирован здесь, чтобы показать одну строку. Спасибо. –
Вы можете отредактировать свой вопрос и добавить соответствующую информацию. Он будет лучше отформатирован, как в комментарии. – LaurentG