I have this class that creates an application context from XML:
public class SpringModel {
public SpringModel(Object dependency) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(...);
Foo foo = (Foo) applicationContext.getBean("foo");
}
}
The bean named "foo" has a constructor that looks like this:
public Foo(Object dependency) {
...
}
I want to inject the argument from SpringModel
's constructor into the constructor for Foo
. Is this possible?
If you're using an xml based application context you could define the dependencies like
<beans>
<bean id="dependencyObj" class="Object"/>
<bean id="springModelObj" class="SpringModel">
<constructor-arg ref="dependencyObj"/>
</bean>
<bean id="foo" class="Foo">
<constructor-arg ref="dependencyObj" />
</bean>
</beans>
It might also be easier to have your Foo object reference be a private variable in the SpringModel class with an appropriate setter method. Then you could link it to the SpringModel bean in the xml configuration with a property tag.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.