在具有共享服务的单个应用程序中将Apache cxf与spring mvc一起使用

rzc*_*zch 6 apache model-view-controller spring hibernate cxf

我目前正在开发一个基于spring MVC的项目,它只是一个使用spring MVC模板的标准项目.所以我有web.xml和servlet-context.xml.

我正在努力将Apache cxf Web服务添加到这个项目中,并且遇到与现有Spring MVC共享服务的一些问题.

我最初的方法是尝试使Web服务正常工作,所以这里的web.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml
        /WEB-INF/spring/jaxwsServlet/jaxwsServlet-context.xml
        </param-value>
    </context-param>




    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Process web service requests -->
    <servlet>
        <servlet-name>jaxws</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jaxws</servlet-name>
        <url-pattern>/industryAspectWS</url-pattern>
    </servlet-mapping>


    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
Run Code Online (Sandbox Code Playgroud)

和我的jaxwsServlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:ws="http://jax-ws.dev.java.net/spring/core"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://jax-ws.dev.java.net/spring/core
        classpath:spring-jax-ws-core.xsd
        http://jax-ws.dev.java.net/spring/servlet
        classpath:spring-jax-ws-servlet.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>   
    <wss:binding url="/industryAspectWS">
        <wss:service>
            <ws:service bean="#industryAspectWS"/>
        </wss:service>
    </wss:binding>

    <!-- Web service methods -->
    <bean id="industryAspectWS" class="com.example.ws.IndustryAspectWS"></bean>

    <context:component-scan base-package="com.example" />

    <beans:import resource="../HibernateTransaction.xml" />

    <beans:import resource="../JaxbMarshaller.xml" />

</beans>
Run Code Online (Sandbox Code Playgroud)

我复制了上下文:component-scan beans:从servlet-context.xml 导入部分

这个配置工作正常,因为我能够启动服务器,调用Web服务并访问servlet和jsp.但是,我注意到,因为我在两个上下文xml文件中都引用了hibernateTransaction.xml,所以有两个会话工厂.

我想在Apache cxf和Spring MVC控制器之间共享所有服务(例如hibernate),所以我尝试将设置放在root-context.xml中,它不起作用.我也尝试在线搜索,但没有找到任何关于共享服务的完整示例.我们是否有可能在两者之间设置共享服务?

=================================================

在我发布这个之后,我已经尝试了一些设置,并想出如果我放了一行

<context:component-scan base-package="com.example" />
Run Code Online (Sandbox Code Playgroud)

<tx:annotation-driven transaction-manager="txManagerExample" />
Run Code Online (Sandbox Code Playgroud)

在servlet-context.xml和jaxwsServlet-context.xml中,它都可以正常工作.所有其他设置可以保留在共享的root-context.xml中

sou*_*ica 7

WSSpringServlet不是CXF.这是地铁.我建议使用CXF.在这种情况下,您将拥有一个CXFServlet但是然后您将在主Spring上下文中创建CXF(由ContextLoaderListener.

它将按如下方式工作:您的主要上下文将包含所有共享bean,包括CXF.您的Servlet上下文只包含您的控制器.由于servlet上下文是主上下文的子级,因此控制器也可以访问主上下文中的所有内容.

请参阅Spring页面内嵌入CXF,CXF Servlet传输页面,以及关于在servlet上下文和主上下文之间共享bean的问题的答案.