使用CXFServlet和Jersey Spring Servlet的父Spring应用程序上下文

时间:2014-04-30 15:08:45

标签: java spring web-applications ejb

我正在研究一些代码,以便在大多数Spring应用程序中使用JEE6 MDB。关于WAS如何比Spring的DefaultMessageListenerContainer更好地控制MDB有一些想法。无论如何,到目前为止,我已经能够将它们全部放在一起,EJB部署,Spring启动,但是当我到达Servlet for CXF和Jersey时,它们都会以相同的相对错误失败。

Caused by: java.lang.IllegalStateException: Context attribute is not of type WebApplicationContext: Root WebApplicationContext: startup date [Tue Apr 29 17:16:26 CDT 2014]; parent: ApplicationContext 'parentContext'
at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:124)
at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:99)
at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:82)
at com.sun.jersey.spi.spring.container.servlet.SpringServlet.getDefaultContext(SpringServlet.java:153)
at com.sun.jersey.spi.spring.container.servlet.SpringServlet.getContext(SpringServlet.java:137)
at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)

Caused by: java.lang.IllegalStateException: Context attribute is not of type WebApplicationContext: Root WebApplicationContext: startup date [Mon Apr 28 16:20:56 CDT 2014]; parent: ApplicationContext 'parentContext'
at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:124)
at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:99)
at org.apache.cxf.transport.servlet.CXFServlet.loadBus(CXFServlet.java:52)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.init(CXFNonSpringServlet.java:66)

要注意:我的CXFServlet位于Jersey的SpringServlet之前,我注释掉了CXF以克服错误。

为了使用SpringBeanAutowiringInterceptor连接EJB和Spring,我有一个WebApplicationContext的父上下文。

的web.xml

    <!-- Tell Spring it's configuration file -->
<display-name>Customer Services</display-name>
<context-param>
    <param-name>parentContextKey</param-name>
    <param-value>parentContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-web.xml</param-value>
</context-param>

<listener>
    <display-name>Customer Services Initializer</display-name>
    <listener-class>customer.maf.CustomerServicesInitializer</listener-class>
</listener>

<listener>
    <display-name>Spring Context Loader</display-name>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <init-param>
        <param-name>config-location</param-name>
        <param-value>/WEB-INF/classes/cxf-beans.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<!-- and more configuration -->

我使用的是Spring 3.0.7.RELEASE。部署到WAS 8.5.5。我以为我会创建自己的CXFServlet并覆盖逻辑,但后来我遇到了Jersey错误并认为我可能做的事情不对。

有没有人遇到过这种情况,以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

parentContextKey和locatorFactorSelector参数是获取/加载父上下文所必需的。当您在耳中进行多次战争时,通常需要父上下文,其中所有战争的公共服务相关代码将在父上下文中。当您使用parentContextkey时,您还需要具有locatorFactorSelector参数。 &#34; locatorFactorySelector&#34;将包含包含spring服务层bean的xml文件。如下所示,它指向beanRefContext.xml。

    <!-- Tell Spring it's configuration file -->
    <display-name>Customer Services</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-web.xml</param-value>
    </context-param>
    <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>parentContext</param-value>
    </context-param>
    <context-param>
         <param-name>locatorFactorySelector</param-name>
         <param-value>classpath:beanRefContext.xml</param-value>
    </context-param>

beanRefContext.xml,它将引用包含服务层bean的service-layer-context.xml。

    <beans>
      <bean id="parentContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
          <list>
            <value>service-layer-context.xml</value>
          </list>
        </constructor-arg>
      </bean>
    </beans>

因此,请确保将适当的bean放在service-layer-context.xml中,并使其成为父上下文,root和dispatcher以及其他Web应用程序上下文可以访问它。如果您认为,您的项目没有任何父上下文,请删除parentContextKey上下文参数。