Load spring application context from string

Can someone show me how to use Spring to load an application context via an XML string instead of a file or class resource?

Thanks,

+6
java spring
source share
3 answers

I found a way to do this.

public MyApplicationContext(String xml, ApplicationContext parent){ super(parent); this.configResources = new Resource[1]; configResources[0] = new ByteArrayResource(xml.getBytes()); refresh(); } private Resource[] configResources; protected Resource[] getConfigResources() { return this.configResources; } 
+3
source share

Use this:

 String contextXML = ...; Resource resource = new ByteArrayResource(contextXML.getBytes()); GenericXmlApplicationContext springContext = new GenericXmlApplicationContext(); springContext.load(resource); Object myBean = springContext.getBean("myBean"); ... 

Reza

+10
source share

Not tried so far, but can probably try:

 // define and open the input stream from which we read the configuration 

InputStream input = new ByteArrayInputStream ("string" .getBytes ("UTF-8")); // create a bean factory DefaultListableBeanFactory beans = new DefaultListableBeanFactory (); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader (beans); reader.setValidationMode (XmlBeanDefinitionReader.VALIDATION_XSD); reader.loadBeanDefinitions (new InputStreamResource (input)); beans.preInstantiateSingletons (); input.close ();

@Ref: http://beradrian.wordpress.com/2010/03/30/load-spring-from-input-stream/ Let me know if this works ...

0
source share

All Articles