在JavaSE和Web应用程序之间共享应用程序上下文 [英] Sharing application context between JavaSE and Web Application

查看:160
本文介绍了在JavaSE和Web应用程序之间共享应用程序上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java SE应用程序,它运行一个托管RESTful Web服务的嵌入式Jetty Web服务器。我想在Java SE部分和Jetty中运行的Web应用程序中使用Spring。一些豆类,例如dao和实体管理器需要在这两个部分之间共享。当我启动应用程序时,我得到bean和实体管理器的重复版本。

I have a Java SE application running an embedded Jetty web server hosting a RESTful web service. I want to use Spring in both the Java SE part and in the web application running in Jetty. Some beans, e.g. a dao and the entity manager, needs to be shared between the two parts. When I start the application I get duplicate versions of both the beans and the entity manager.

WARN EntityManagerFactoryRegistry.addEntityManagerFactory:80 - HHH000436: Entity manager factory name (persistenceUnit) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'

在Java SE部分中,我要做的第一件事就是创建应用程序上下文。

In the Java SE part the first thing I do is to create the application context.

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:server-context.xml","classpath:application-context.xml");

在Web应用程序中,Spring在web.xml中配置:

In the web application Spring is configured in web.xml:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
        <param-value>example.restful.server.filter.SecurityFilterFactory</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
        <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>example.restful.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

如何让Web应用程序使用已创建的应用程序上下文而不是复制所有bean?

How can I make the web application use the application context already created instead of duplicating all the beans?

推荐答案

我认为您可以创建自己的 ContextLoaderListener 并覆盖 createWebApplicationContext 返回JavaSE应用程序的ApplicationContext(假设你有某种ApplicationContextHolder来保持对它的静态访问)。

I think you can create your own ContextLoaderListener and overriding createWebApplicationContext to return the ApplicationContext of your JavaSE app (assuming you have some kind of ApplicationContextHolder to keep a static access to it).

看起来很简单,但你需要返回一个 WebApplicationContext 而不是 ClassPathXmlApplicationContext 来做这个,我想你可以做这样的事情:

It seems easy, but you need to return a WebApplicationContext and not a ClassPathXmlApplicationContext to do this, I think you can do something like this:

public class MyContextLoaderListener extends ContextLoaderListener{
    protected WebApplicationContext createWebApplicationContext(ServletContext sc,
                                                        ApplicationContext parent){
        ApplicationContext javaSEAppContext = AppContextHolder.getAppContext();
        GenericWebApplicationContext context = new GenericWebApplicationContext(servletContext);
        context.setParent(javaSEAppContext);
        return context;
    }
}

当然要调整你的web.xml

And of course adapt your web.xml

    <!-- <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param> -->
    <listener>
        <listener-class>com.company.MyContextLoaderListener</listener-class>
    </listener>

这篇关于在JavaSE和Web应用程序之间共享应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆