This is my class :
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()); This is the relevant xml configuration:
<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 is assigned correctly as String TWO. I am getting NumberFormatException when injecting a TWO_INTEGER. Is there a way to achieve this without adding a setter that takes String and coverts it to int in Controller2 class?
The error :
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}" Thanks.
PropertyPlaceholderConfigureris obviously not finding the properties files or the properties files do not contain any property like${TWO_INTEGER}. Have you check this?ONE_MESSAGE=ONEand two.properties has :TWO_MESSAGE=TWO TWO_INTEGER=30and 'TWO_MESSAGE' property is read correctly. Just, to clarify. there are two lines in two.properties not the way its formatted here to show one line.Thanks.