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

Sea*_*yen 6 java spring

有人可以告诉我如何使用Spring通过xml字符串而不是文件或类路径资源来加载应用程序上下文?

谢谢,

Rez*_*bri 11

用这个:

String contextXML = ...;
Resource resource = new ByteArrayResource(contextXML.getBytes());
GenericXmlApplicationContext springContext = new GenericXmlApplicationContext();
springContext.load(resource);
Object myBean = springContext.getBean("myBean");
...
Run Code Online (Sandbox Code Playgroud)

雷扎


Sea*_*yen 3

我找到了一种方法。

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;
}
Run Code Online (Sandbox Code Playgroud)