I have a FileSystemXmlApplicationContext
and I would like the beans defined in the XML to take as a constructor argument a bean which is not declared in Spring
How about programmatically creating an empty parent context first, registering your object as a singleton with that context's BeanFactory
using the fact that getBeanFactory
returns an implementation of SingletonBeanRegistry
.
parentContext = new ClassPathXmlApplicationContext();
parentContext.refresh(); //THIS IS REQUIRED
parentContext.getBeanFactory().registerSingleton("myBean", myBean)
Then specify this context as a parent to your "real" context The beans in the child context will then be able to refer to the bean in the parent.
String[] fs = new String[] { "/path/to/myfile.xml" }
appContext = new FileSystemXmlApplicationContext(fs, parentContext);