Question

As the title says, I want to load some beans from a String. This approach only works in Spring 3 because GenericXmlApplicationContext is unavailable in version 2.5.

Was it helpful?

Solution

Use the approach outlined here. With one additional step, pass the DefaultListableBeanFactory into a GenericApplicationContext (this one has been around since Spring 1.1 and the GenericXmlApplicationContext is basically a convenience class doing more or less the same as in that blog post).

So something like this should work

String content = ...
GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx);
reader.loadBeanDefinitions(new ByteArrayResource(content.getBytes())); 
ctx.refresh();

The ApplicationContext should now be ready for use.

OTHER TIPS

I have not tested it myself, but according to the API documentation, you can easily extend the AbstractXmlApplicationContext and implement the getConfigResources() method. You can use the org.springframework.core.io.ByteArrayResource to serve as a placeholder for your XML string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top