cxf框架和spring mvc的集成

版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/zhihui1017/article/details/50781670

cxf和spring mvc的集成


Apache CXF 是一个开源的 Services 框架,CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。

Spring MVC是通过DispatcherServlet来加载Spring配置文件的,因此不需要在web.xml中配置ContextLoaderListener。但是CXF却需要通过ContextLoaderListener来加载Spring。 

这样就产生了一个矛盾,如果不配置ContextLoaderListener,CXF就无法正常使用。但如果配置ContextLoaderListener,又会造成Spring的重复加载(DispatcherServlet一次,ContextLoaderListener一次) 

在网上查了一下资料,只看到一个国外的程序员提出不配置ContextLoaderListener,通过写一个CXFController,来替代默认的CXFServlet。但是这种HACK的方式总是不太好 

所以我采用一种折中的方式,来解决Spring MVC和cxf并存的问题。但是也不是很优雅,希望有人能给出更好的办法 

首先是web.xml的配置 
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 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_3_0.xsd">

    <!--<absolute-ordering/>-->
    <!-- 配置spring-servlet的控制容器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- 配置Spring监听 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 加载Spring主容器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <!-- WebService配置CXF -->
    <servlet>
        <servlet-name>CXF-WebService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXF-WebService</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
在ContextLoaderListener里面,加载cxf和spring的公共配置信息。然后在DispatcherServlet中加载spring mvc所需的信息。利用Spring配置文件拆分的方式,既实现spring mvc和cxf的共存,又避免spring配置信息的重复加载

然后是spring公共配置信息:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                            http://www.springframework.org/schema/context   
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                            http://www.springframework.org/schema/tx  
                            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                            
                             http://camel.apache.org/schema/spring 
                             http://camel.apache.org/schema/spring/camel-spring.xsd ">
       <import resource="cxf.xml"/>
                         
       <context:component-scan base-package="com.webapp.service"/>                     
       <context:component-scan base-package="com.webapp.webservice"/>

</beans>

然后是cxf客户端所需的信息,cxf.xml 
<?xml version="1.0" encoding="UTF-8"?>  
  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                            http://cxf.apache.org/jaxws   
                            http://cxf.apache.org/schemas/jaxws.xsd">  
  
   <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>/WEB-INF/webservice_address.properties</value>  
            </list>  
        </property>  
    </bean>  
  
<!-- 客户端配置-->
< jaxws:client id ="Client" serviceClass ="com.webapp.webservice.AssembleBillInterface" address ="${Client}" />
<!-- 服务端配置
       <jaxws:endpoint id="helloWorld"  
           implementorClass="com.framework.webservice.HelloWorldImpl"  
        address="/allbillservice" />  -->
</ beans>
自定义配置信息:webservice_address.properties
 
  
 
  
Client = http://127.0.0.1:28081/datamanager/webservices/allbillservice?wsdl

springMvc 配置springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"   
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
                                http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                                http://www.springframework.org/schema/context   
                                http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                                http://www.springframework.org/schema/mvc
                                http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
      
      <!--controller包中进行扫描,完成Bean创建和自动依赖注入的功能-->
      <context:component-scan base-package="com.webapp.controller"/>

      <!-- 完成请求和注解POJO的映射 -->
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
      
      <!-- Default ViewResolver -->
      <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="viewClass"
                    value="org.springframework.web.servlet.view.JstlView" />
           <property name="prefix" value="/" />
           <property name="suffix" value=".jsp"></property>
      </bean>


</beans>


猜你喜欢

转载自blog.csdn.net/zhihui1017/article/details/50781670
今日推荐