问题描述
我有一个独立的spring项目,因此需要启动一个嵌入式rest服务.我可以用grizzly启动服务器,我的问题是,当我启动grizzly服务器时,它会创建自己的应用程序上下文.因此由我的父应用程序创建的实例无法通过REST服务访问.
I have a standalone spring project and i need to start an embedded rest service with it.I could be able to start the server with grizzly, my problem is, when i start grizzly server, it creates its own application context. so the instances created by my parent application is not accessible through the REST service.
除了获得grizzly生成的应用程序上下文之外,是否还有在Grizzly服务器和父级应用程序之间共享父应用程序的上下文的信息.
Is there anyway of sharing the parent application's context between Grizzly server and parent application, other than getting grizzly generated application context.
这是我启动灰熊服务器的代码.
This is my code for starting the grizzly server.
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
的父级.然后我得到以下异常.
I tried setting current context as cac
's parent too. Then i got following exception.
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
谢谢.
推荐答案
尝试一下:
ConfigurableApplicationContext cac =
new ClassPathXmlApplicationContext("classpath:remoting-context.xml");
// Have Spring load the context
cac.refresh();
IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc, cac);
这篇关于灰熊,分享春天产生的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!