Back Forum Reply New

Using PropertyPlaceholderConfigurer for integer values

Hi All,

I have a simple 'Employee' bean where the constructor takes in an integer. I am trying to create an instance of Employee at run-time by passing values using 'PropertyPlaceholderConfigurer'. How could i pass in integer values using 'PropertyPlaceholderConfigurer' ?

Thanks in advance.

Exception ...

Code:
Error creating bean with name 'e1' defined in file
[C:\workspace\MyTest\config\applicationContext.xml]:
Unsatisfied dependency expressed through constructor
argument with index 1 of type [int]: Could not convert
constructor argument value of type [java.lang.String] to required type [int]:
Failed to convert value of type [java.lang.String] to required type [int];
nested exception is
java.lang.NumberFormatException: For input string: quot;${emp.age}quot;
applicationContext.xml ...

Code:

lt;bean id=quot;e1quot; class=quot;test.Employeequot; abstract=quot;falsequot;
lazy-init=quot;defaultquot; autowire=quot;defaultquot; dependency-check=quot;defaultquot;gt;
lt;constructor-arg index=quot;0quot; type=quot;java.lang.Stringquot;gt;
lt;valuegt;${emp.name}lt;/valuegt;
lt;/constructor-arggt;
lt;constructor-arg index=quot;1quot; type=quot;intquot;gt;
lt;valuegt;${emp.age}lt;/valuegt;
lt;/constructor-arggt;
lt;/beangt;

Code:
Properties props2 = new Properties();
props2.setProperty(quot;emp.namequot;, quot;sdurvasulaquot;);
props2.setProperty(quot;emp.agequot;, quot;30quot;);

PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setProperties(props2);

FileSystemXmlApplicationContext appCtx = new FileSystemXmlApplicationContext(LOC);
appCtx.addBeanFactoryPostProcessor(cfg);
appCtx.refresh();

Employee e1 = (Employee) appCtx.getBean(quot;e1quot;);

System.out.println(quot;Age : quot; + e1.getAge());
System.out.println(quot;Name : quot; + e1.getName());
Bean ...
Code:
public class Employee {

private String name = null;

private int age = 0;

public Employee(String name, int age) {
setName(name);
setAge(age);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}First off all don't work with the beans directly, simply configure it in your xml file. Next to that I seriously doubt if this is what you want to do, instantiating/configuring your Employee inside a ApplicationContext. Code:
lt;bean id=quot;e1quot; class=quot;test.Employeequot;gt;
lt;constructor-arg index=quot;0quot; type=quot;java.lang.Stringquot; value=quot;${emp.name}quot;/gt;
lt;constructor-arg index=quot;1quot; type=quot;intquot; value=quot;${emp.age}quot;/gt;
lt;/beangt;

lt;bean class=quot;org.springframwork.beans.factory.config.PropertyPlaceHolderConfigurerquot;gt; lt;property name=quot;propertiesquot;gt;
lt;propsgt; lt;prop key=quot;emp.namequot;gt;sdurvasulalt;/propgt; lt;prop key=quot;emp.agequot;gt;30lt;/propgt;
lt;/propsgt; lt;/propertygt;
lt;/beangt;Thanks a lot for your quick response Marten. The issue is that, the properties quot;employee namequot; and quot;employee agequot; are available only at runtime through a pop-up dialog. So i cannot wire then into the XML or a properties file. Thats why i was creating a quot;java.util.Propertiesquot; instance, filling in the properties and adding the instance to 'PropertyPlaceHolderConfigurer'. But this procedure falls apart if any of the bean properties are integers.

Bean 'e1' will be injected as a dependency to another bean using dependency injection.

Best Regards.

You can have the properties in a .properties file:

Code:
lt;bean id=quot;propertyPlaceholderConfigurerquot; class=quot;org..beans.factory.config.PropertyPlaceholderConfigurerquot;gt;
lt;property name=quot;locationquot; value=quot;classpath:employee.propertiesquot;gt;lt;/propertygt;
lt;property name=quot;ignoreResourceNotFoundquot; value=quot;truequot;gt;lt;/propertygt;
lt;/beangt;
¥
Back Forum Reply New