2013-06-02 23 views
2

我有一个独立的弹簧项目,我需要启动一个嵌入式休息服务。 我可以能够与灰熊启动服务器,我的问题是,当我开始灰熊的服务器,它会创建自己的应用程序上下文。所以我的父应用程序创建的实例无法通过REST服务访问。灰熊,共享弹簧产生的上下文

在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

感谢。

回答

0

试试这个:

ConfigurableApplicationContext cac = 
      new ClassPathXmlApplicationContext("classpath:remoting-context.xml"); 
// Have Spring load the context 
cac.refresh(); 
IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc, cac);