我有一个独立的 spring 项目,我需要用它启动一个嵌入式休息服务。
我可以用 grizzly 启动服务器,我的问题是,当我启动 grizzly 服务器时,它会创建自己的应用程序上下文。因此我的父应用程序创建的实例无法通过 REST 服务访问。
除了获取 grizzly 生成的应用程序上下文之外,是否可以在 Grizzly 服务器和父应用程序之间共享父应用程序的上下文。
这是我启动 grizzly 服务器的代码。
public class RemotingServer {
private HttpServer httpServer;
private String host;
private int port;
public RemotingServer(String host, int port) {
this.host = host;
this.port = port;
}
public void init() throws Exception {
URI uri = UriBuilder.fromUri("http://" + host + "/").port(port).build();
ResourceConfig rc = new DefaultResourceConfig();
ConfigurableApplicationContext cac =
new ClassPathXmlApplicationContext("classpath:remoting-context.xml");
IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc, cac);
httpServer = GrizzlyServerFactory.createHttpServer(uri, rc, factory);
httpServer.start();
}
public void stop() {
httpServer.stop();
}
}
我尝试将当前上下文设置为cac
的家长也是。然后我得到了以下异常。
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
thanks.
尝试这个:
ConfigurableApplicationContext cac =
new ClassPathXmlApplicationContext("classpath:remoting-context.xml");
// Have Spring load the context
cac.refresh();
IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc, cac);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)