spring整合cxf开发webservice

来源:互联网 发布:linux nls 简体中文 编辑:程序博客网 时间:2024/06/03 19:53

我们开发webservice接口时候,一般都是web工程,之前联调的时候,就和一个IBM的联调过一个OA接口,他们使用的是Spring整合axis  持久层用的hibernate

   下面我们来整合下spring与cxf

  建立一个web工程 ,引入cxf  lib下面的jar包

  配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?><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">  <display-name>webservice_cxf_spring_day01</display-name>    <!--加载spring容器-->   <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:beans.xml</param-value>  </context-param>      <!--配置监听器-->   <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>       <!--配置CXF-->   <servlet>          <servlet-name>CXFServlet</servlet-name>          <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>          <load-on-startup>1</load-on-startup>      </servlet>        <servlet-mapping>          <servlet-name>CXFServlet</servlet-name>          <url-pattern>/*</url-pattern>      </servlet-mapping>      <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
beans.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:jaxws="http://cxf.apache.org/jaxws"      xmlns:cxf="http://cxf.apache.org/core"      xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd       http://cxf.apache.org/jaxws       http://cxf.apache.org/schemas/jaxws.xsd">            <import resource="classpath:META-INF/cxf/cxf.xml" />            <jaxws:endpoint id="userService" implementor="com.asiainfo.webservice.service.impl.UserServiceImpl" address="/userService" />    </beans> 
SEI:

package com.asiainfo.webservice.service.interfaces;import javax.jws.WebMethod;import javax.jws.WebService;import com.asiainfo.webservice.pojo.User;@WebServicepublic interface IUserService {@WebMethodpublic void printUserName(String username);@WebMethodpublic User queryUserInfo(int id);@WebMethodpublic int addUserInfo(User user);}
SEI实现类

package com.asiainfo.webservice.service.impl;import javax.jws.WebService;import com.asiainfo.webservice.pojo.User;import com.asiainfo.webservice.service.interfaces.IUserService;@WebServicepublic class UserServiceImpl implements IUserService{public void printUserName(String username){System.out.println(username);}public User queryUserInfo(int id){return new User(1,"曾孟良","123456");}public int addUserInfo(User user){System.out.println(user.toString());return 0;}}
启动tomcat ,在启动tomcat的时候,其实就发布了webservice,你可以给UserServiceImpl ,加一个无参构造器。在tomcat启动时候,这个类就实例化了

  测试结果:

   

  说明已经整合成功

 

0 0