Grizzly, sharing spring generated context

与世无争的帅哥 提交于 2019-12-08 01:38:55

问题


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.

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

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

thanks.


回答1:


Try this:

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


来源:https://stackoverflow.com/questions/16885280/grizzly-sharing-spring-generated-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!