2

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.

6
  • 1
    The PropertyPlaceholderConfigurer is obviously not finding the properties files or the properties files do not contain any property like ${TWO_INTEGER}. Have you check this? Commented Aug 2, 2013 at 19:27
  • I checked this. In fact, other String properties from the same file are getting assigned correctly. one.properties has : ONE_MESSAGE=ONE and two.properties has : TWO_MESSAGE=TWO TWO_INTEGER=30 and '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. Commented Aug 2, 2013 at 19:34
  • You could edit your question and add this relevant information. It will be better formatted as in a comment. Commented Aug 2, 2013 at 19:40
  • Why you can't define PropertyPlaceholderConfigurer in xml file? Commented Aug 2, 2013 at 19:43
  • LaurentG : edited the question. @bellabax : The properties file names are decided at run time. Unless there is a way to define it in xml file and then change it programatically, I have to do it this way. Thanks. Commented Aug 2, 2013 at 19:45

1 Answer 1

5

Probably your application falling in this line (please provide full stacketrace if i mistake):

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

because Spring can't parse ${TWO_INTEGER} (this properties doesn't loaded in context as yet). So you can just move context initializing after properties loaded:

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); PropertyPlaceholderConfigurer pph = new PropertyPlaceholderConfigurer(); pph.setLocations(new Resource[]{new ClassPathResource("one.properties"), new ClassPathResource("two.properties")}); context.addBeanFactoryPostProcessor(pph); context.setConfigLocation("beans.xml"); context.refresh(); 

Hope this help.

Sign up to request clarification or add additional context in comments.

1 Comment

It works now. Thanks a lot for the solution and the explanation. Really appreciate it. Curious : Why are the String properties read correctly even when they are in the same file?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.