简体   繁体   English

从字符串加载spring应用程序上下文

[英]load spring application context from string

Can somebody show me how to use Spring to load application context through xml string instead of file or classpath resource? 有人可以告诉我如何使用Spring通过xml字符串而不是文件或类路径资源加载应用程序上下文?

Thanks, 谢谢,

Use this: 用这个:

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

Reza 雷扎

i found a way doing it. 我找到了一种方法。

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;
}

Haven't tried it so far but can probably give a try: 到目前为止还没试过,但可以尝试一下:

// define and open the input stream from which we read the configuration
InputStream input = new ByteArrayInputStream("string".getBytes("UTF-8"));
// create the 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 it works... @Ref: http ://beradrian.wordpress.com/2010/03/30/load-spring-from-input-stream/让我知道它是否有效......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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