CXF整合spring

近公司需要弄webservics,还说不用框架整合(提倡使用hessian,他们既然说与操作系统有兼容问题,由于人员单薄,不得不屈服,哎),我想了老半天没弄明白他说的不用框架整合spring,尝试过直接使用Endpoint.publish("http://127.0.0.1:6789/hello", new HelloService());【第一个参数是暴露借口的url,参数+?wsdl就可以获取wsdl文档;第二个参数是要对外发布的服务器,不是接口】由于服务器是直接new出来的,不归spring管理,所以在获取系统的中services时候,注入不了,网上各种搜索,也尝试过用Axis2整合spring,但是好像挺麻烦的。

导入CXF与spring整合需要的jar(这事三大框架整合过后的jar):

cxf-2.4.2.jar

geronimo-annotation_1.0_spec-1.1.1.jar

geronimo-jaxws_2.2_spec-1.0.jar

geronimo-jms_1.1_spec-1.1.1.jar

geronimo-servlet_3.0_spec-1.0.jar

geronimo-ws-metadata_2.0_spec-1.1.3.jar

jaxb-api-2.2.1.jar

jaxb-impl-2.2.1.1.jar

neethi-3.0.1.jar

wsdl4j-1.6.2.jar

xmlschema-core-2.0.jar

两种方式发布服务,一种是接口,一种是非接口

 接口发布:

 1 package com.yidu.zh.cxf.service;
 2 
 3 import javax.jws.WebService;
 4 import javax.xml.ws.BindingType;
 5 import javax.xml.ws.soap.SOAPBinding;
 6 @WebService
 7 //@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)//JDK1.6以上才支持1.2,否则会报错:org.apache.cxf.binding.soap.SoapFault
 8 public interface HiService {  9 public String sayHi(String name); 10 }

接口实现类:

package com.yidu.zh.cxf.service;

public class HiServiceImpl implements HiService {

    @Override
    public String sayHi(String name) {
        System.out.println("正在执行方法!");
        return "hello"+name;
    }

}

spring配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6           http://www.springframework.org/schema/beans/spring-beans.xsd
 7             http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
 8             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9             http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
10     <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
11     <import resource="classpath:META-INF/cxf/cxf.xml" />
12     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
13     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
14  
15     <!-- 方式二:有接口的方式 -->
16     <!--
17       ID:标识唯一
18       serviceClass:接口类型(发布的服务器类;必须加上@WebService注解)
19       address:服务请求url(结合web.xml的映射就是:http://localhost:8080/项目名称/cxf/hi?wsdl)
20       -->
21     <jaxws:server id="hiService" serviceClass="com.yidu.zh.cxf.service.HiService" address="hi">
22         <!-- 提供接口的实现类;服务的实现类 -->
23         <jaxws:serviceBean>
24             <bean class="com.yidu.zh.cxf.service.HiServiceImpl"></bean>
25         </jaxws:serviceBean>
26         <!-- 加入请求的消息拦截器 -->
27         <jaxws:inInterceptors>
28             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
29         </jaxws:inInterceptors>
30         <!-- 加入响应的消息拦截器 -->
31         <jaxws:outInterceptors>
32             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
33         </jaxws:outInterceptors>
34     </jaxws:server>
35     
36     
37      
38 </beans>

web.xml配置(发布服务的两种在方式在web.xml配置都一样):

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>CXF_Web_WebService</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11     <!-- 配置CXF框架 -->
12   </welcome-file-list>
13   <!-- 通过上下文参数指定spring配置文件的位置 -->
14   <context-param>
15       <param-name>contextConfigLocation</param-name>
16       <param-value>classpath:cxf-servlet.xml</param-value>
17   </context-param>
18   <listener>
19       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
20   </listener>
21   
22   <servlet>
23       <servlet-name>cxf</servlet-name>
24       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
25   </servlet>
26   
27   <servlet-mapping>
28       <servlet-name>cxf</servlet-name>
29       <url-pattern>/cxf/*</url-pattern>
30   </servlet-mapping>
31   
32 </web-app>

无接口方式:

服务类:

 1 package com.yidu.zh.cxf.service;
 2 
 3 import javax.jws.WebService;
 4 import javax.xml.ws.BindingType;
 5 import javax.xml.ws.soap.SOAPBinding;
 6 
 7 /**
 8  * 
 9  * @author Administrator
10  *
11  */
12 @WebService
13 //@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)//修改服务器版本为1.2,JDK1.6以上才支持1.2,否则会报错:org.apache.cxf.binding.soap.SoapFault
14 public class HelloService {
15     public String sayHello(String name){
16         System.out.println("正在执行服务器中的方法");
17         return "hello " + name;
18     }
19 }

 spring配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6           http://www.springframework.org/schema/beans/spring-beans.xsd
 7             http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
 8             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9             http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
10     <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
11     <import resource="classpath:META-INF/cxf/cxf.xml" />
12     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
13     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
14     <!-- 方式一;没有借口的方式(简单方式) -->
15     <!-- 
16         ID:唯一标识
17         implementor:借口的实现的;这里没有接口,直接写类(也就是发布的服务器;该类或接口必须使用@WebService注解)
18         address:访问的资源名称(通过工程的名称加上该属性值获取wsdl文件)
19     -->
20     <jaxws:endpoint id="helloService" implementor="com.yidu.zh.cxf.service.HelloService" address="hello">
21         <!-- 加入请求的消息拦截器 -->
22         <jaxws:inInterceptors>
23             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
24         </jaxws:inInterceptors>
25         <!-- 加入响应的消息拦截器 -->
26         <jaxws:outInterceptors>
27             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
28         </jaxws:outInterceptors>
29     </jaxws:endpoint>
30     
31      
32 </beans>
原文地址:https://www.cnblogs.com/cunkouzh/p/6083127.html