View Full Version : PropertyPlaceholderConfigurer not working with inner bean?
cmgharris
Aug 31st, 2004, 07:23 AM
I currently using Spring ver. 1.0.2
I am using PropertyPlaceholderConfigurer successfully in a number of places, but have found that it doesn't seem to work on an inner bean.
Thus, the following works fine:
<bean id="ifxds" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>${informixJndiName}</value></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref bean="ifxds"/>
</constructor-arg>
</bean>
But the following doesn't:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>${informixJndiName}</value></property>
</bean>
</constructor-arg>
</bean>
giving the following error:
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.jndi.JndiObjectFactoryBean' defined in resource/WEB-INF/config/wo_bus_config.xml] of ServletContext: Initialization of bean failed; nested exception is javax.naming.NameNotFoundException: ${informixJndiName} not bound javax.naming.NameNotFoundException: ${informixJndiName} not bound
at org.jnp.server.NamingServer.getBinding(NamingServe r.java:495)
at org.jnp.server.NamingServer.getBinding(NamingServe r.java:503)
at org.jnp.server.NamingServer.getObject(NamingServer .java:509)
at org.jnp.server.NamingServer.lookup(NamingServer.ja va:282)
at org.jnp.interfaces.NamingContext.lookup(NamingCont ext.java:530)
at org.jnp.interfaces.NamingContext.lookup(NamingCont ext.java:509)
at javax.naming.InitialContext.lookup(InitialContext. java:345)
at org.springframework.jndi.JndiTemplate.doInContext( JndiTemplate.java:120)
etc. etc..
Is this correct behaviour? If so, what is the reason?
Thanks
Alef Arendsen
Aug 31st, 2004, 09:25 AM
It works for all inner beans mentioned as properties, but not for constructor arguments. Adding support for constructor arguments shouldn't that much of an issue.
Juergen, if you're reading this, I'll have a look at this. I'll keep you posted.
Alef Arendsen
Aug 31st, 2004, 09:33 AM
Errr, too quick. Ignore my previous post. I'll have another look at it ;-)
Alef Arendsen
Aug 31st, 2004, 10:25 AM
Chris, could you have a look at the isolated test I created. I've attached everything you need to the following JIRA issue.
I'm using the following context:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="testBean" class="org.springframework.beans.TestBean">
<constructor-arg>
<bean class="org.springframework.beans.TestBean">
<property name="age"><value>${age}</value></property>
</bean>
</constructor-arg>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer">
<property name="properties">
<props>
<prop key="age">98</prop>
</props>
</property>
</bean>
</beans>
Juergen Hoeller
Aug 31st, 2004, 02:59 PM
Alef, do you already have any insights on this issue? I'm a bit puzzled that this didn't work for constructor arguments before...
Juergen
Alef Arendsen
Sep 1st, 2004, 02:34 AM
Err, I forgot to post a link to the JIRA issue. At first sight I thought (from the code) I thougth it was indeed not working for beans passed as constructor arguments, but I've written a test proving that it does.
http://opensource.atlassian.com/projects/spring/browse/SPR-302
So unless Chris confirms that it still doesn't work using his config, let's say it works.
cmgharris
Sep 1st, 2004, 03:07 AM
I've just tried this, and it still doesn't work for me.
I changed your TestBean slightly, as I was missing a couple of interfaces, but not so as to affect the test, I don't think.
Used the following to test it:
public class Runner {
public static void main(String[] args) {
Runner runner = new Runner();
runner.testPropertyPlaceholderConfigurerWithInnerB eansInConstructorArgs();
}
public void testPropertyPlaceholderConfigurerWithInnerBeansInC onstructorArgs() {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("test.xml");
ctx.refresh();
TestBean tb = (TestBean)ctx.getBean("testBean");
TestBean spouse = tb.getSpouse();
//assertEquals(98, spouse.getAge());
System.out.println("spouse.getAge gives " + spouse.getAge());
}
}
I got the following result:
/usr/j2sdk1.4.2_05/bin/java -Dfile.encoding=US-ASCII -classpath /usr/j2sdk1.4.2_05/jre/lib/sunrsasign.jar:/usr/j2sdk1.4.2_05/jre/lib/jce.jar:/usr/j2sdk1.4.2_05/jre/lib/charsets.jar:/usr/j2sdk1.4.2_05/jre/lib/plugin.jar:/usr/j2sdk1.4.2_05/jre/lib/rt.jar:/usr/j2sdk1.4.2_05/jre/lib/jsse.jar:/usr/j2sdk1.4.2_05/jre/lib/ext/sunjce_provider.jar:/usr/j2sdk1.4.2_05/jre/lib/ext/dnsns.jar:/usr/j2sdk1.4.2_05/jre/lib/ext/ldapsec.jar:/usr/j2sdk1.4.2_05/jre/lib/ext/localedata.jar:/var/home/dp01/configtest:/user1/spring-framework-1.0.2/dist/spring.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-attributes-api-SNAPSHOT.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-attributes-compiler-SNAPSHOT.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-beanutils.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-collections.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-dbcp.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-digester.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-discovery.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-fileupload.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-lang.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-logging.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-pool.jar:/user1/spring-framework-1.0.2/lib/jakarta-commons/commons-validator.jar org.springframework.beans.Runner
Sep 1, 2004 8:54:50 AM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [test.xml]
Sep 1, 2004 8:54:51 AM org.springframework.context.support.AbstractXmlApp licationContext refreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlA pplicationContext;hashCode=605645]: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [testBean,org.springframework.beans.factory.config .PropertyPlaceholderConfigurer]; Root of BeanFactory hierarchy
Sep 1, 2004 8:54:51 AM org.springframework.context.support.AbstractApplic ationContext refresh
INFO: 2 beans defined in ApplicationContext [org.springframework.context.support.ClassPathXmlA pplicationContext;hashCode=605645]
Sep 1, 2004 8:54:51 AM org.springframework.beans.factory.support.Abstract BeanFactory getBean
INFO: Creating shared instance of singleton bean 'org.springframework.beans.factory.config.Property PlaceholderConfigurer'
Sep 1, 2004 8:54:51 AM org.springframework.context.support.AbstractApplic ationContext initMessageSource
INFO: No MessageSource found for context [org.springframework.context.support.ClassPathXmlA pplicationContext;hashCode=605645]: using empty StaticMessageSource
Sep 1, 2004 8:54:51 AM org.springframework.context.support.AbstractApplic ationContext refreshListeners
INFO: Refreshing listeners
Sep 1, 2004 8:54:51 AM org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.Default ListableBeanFactory defining beans [testBean,org.springframework.beans.factory.config .PropertyPlaceholderConfigurer]; Root of BeanFactory hierarchy]
Sep 1, 2004 8:54:51 AM org.springframework.beans.factory.support.Abstract BeanFactory getBean
INFO: Creating shared instance of singleton bean 'testBean'
Sep 1, 2004 8:54:51 AM org.springframework.beans.factory.support.Abstract BeanFactory destroySingletons
INFO: Destroying singletons in factory {org.springframework.beans.factory.support.Default ListableBeanFactory defining beans [testBean,org.springframework.beans.factory.config .PropertyPlaceholderConfigurer]; Root of BeanFactory hierarchy}
Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.beans.TestBean' defined in class path resource [test.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptions Exception: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchExceptio n: Failed to convert property value of type [java.lang.String] to required type [int] for property 'age'; nested exception is java.lang.NumberFormatException: For input string: "${age}"]
PropertyAccessExceptionsException (1 errors)
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'age'; nested exception is java.lang.NumberFormatException: For input string: "${age}"
java.lang.NumberFormatException: For input string: "${age}"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.valueOf(Integer.java:574)
at sun.beans.editors.IntEditor.setAsText(IntEditor.ja va:21)
at org.springframework.beans.BeanWrapperImpl.doTypeCo nversionIfNecessary(BeanWrapperImpl.java:828)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:637)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:568)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:685)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:712)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:701)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:626)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:467)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:232)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.resolveInnerBeanDefinit ion(AbstractAutowireCapableBeanFactory.java:702)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.resolveValueIfNecessary (AbstractAutowireCapableBeanFactory.java:663)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:307)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:214)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:177)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:159)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:183)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:268)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationContext.java:58)
at org.springframework.beans.Runner.testPropertyPlace holderConfigurerWithInnerBeansInConstructorArgs(Ru nner.java:20)
at org.springframework.beans.Runner.main(Runner.java: 16)
Process finished with exit code 1
Alef Arendsen
Sep 1st, 2004, 04:27 AM
Forget to ask, you're still using 1.0.2.
I haven't tried this with 1.0.2, only with the current cvs version.
I'll have a look later on with 1.0.2. Can't do that now, not at the office.
cmgharris
Sep 1st, 2004, 04:32 AM
Yes, I'm using 1.0.2
Waiting for 1.1 Final before I upgrade.
Alef Arendsen
Sep 1st, 2004, 09:19 AM
Confirmed
Well, I've run the test against 1.0.2 and it produced the exact same error as mentioned by Chris.
And I'm not surprised after having a look at the sources. The 1.0.2 ProperyPlaceHolderConfigurer simply doesn't the overriding of placeholders in child bean definitions.
So Chris, unfortunately you'll have to wait until 1.1, which is going to be released this or next week anyway.
cheers,
Alef
cmgharris
Sep 1st, 2004, 09:25 AM
No problem.
Thanks for your time!
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.