I am trying to autowire a bean from the following context:
It seens that 'stringProperty' using constructor injection is not properly wired.Code:<!-- AutowireTest2.xml --> <bean id="stringProperty" class="java.lang.String"> <constructor-arg value="Hello from String"/> </bean> <bean id="beanProperty" class="MyBean"> <property name="stringProperty" value="Hello from MyBean"/> </bean>
The other property is ok.
---
Here, in essence, is my setup:
---
Code:import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; public class AutowireTest2 { private String stringProperty; public String getStringProperty() { return stringProperty; } public void setStringProperty( String stringProperty ) { this.stringProperty = stringProperty; } private MyBean beanProperty; public MyBean getBeanProperty() { return beanProperty; } public void setBeanProperty( MyBean beanProperty ) { this.beanProperty = beanProperty; } public static void main( String[] args ) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "AutowireTest2.xml" ); AutowireTest2 app = new AutowireTest2(); int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; boolean dependencyCheck = false; // Autowire by name ctx.getAutowireCapableBeanFactory(). autowireBeanProperties(app, autowireMode, dependencyCheck); System.out.println( ctx.getBean( "stringProperty" ) ); System.out.println( app.getStringProperty() ); System.out.println( app.getBeanProperty().getStringProperty() ); } }The output is:Code:public class MyBean { private String stringProperty; public String getStringProperty() { return stringProperty; } public void setStringProperty( String stringProperty ) { this.stringProperty = stringProperty; } }
Hello from String
null
Hello from MyBean
Instead of the null output, should it not say 'Hello from String' ?
Behavior is the same no matter whether I try autowiring by name or by type.
Can anyone help?
Thanks,
Hubert


Reply With Quote
).
