简体   繁体   中英

How to load application context from string in Spring 2.5?

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.

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.

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM