webservice学习之cxf与spring的整合

第一步:

    写一个接口如下:
       package test.wervice;

        import javax.jws.WebService;

        @WebService
        public interface HelloWorld {
            void sayHi(String name);
        }

第二步:

   写一个接口的实现类:
    package test.wervice.impl;

    import javax.jws.WebService;

    import test.wervice.HelloWorld;

    @WebService(endpointInterface="test.wervice.HelloWorld")
    public class HelloWorldImpl implements HelloWorld{

        public void sayHi(String name) {
            System.out.println("helloWorld"+name);
        }
    }

第三步:因为是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">
        <!-- 配置application的加载 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
      </context-param>
      <!-- spring的监听器 -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- 配置cxf的servlet -->
       <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>/cxf/*</url-pattern>  
        </servlet-mapping>  
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

第四步:在web-inf下面创建applicationContext.xml

<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">  
    <!-- 实现接口的bean -->
    <bean id="hello" class="test.wervice.impl.HelloWorldImpl"/>

    <!-- 发布接口的地址和其实现类的bean关联 -->
    <jaxws:endpoint address="/HelloWorld" >
        <jaxws:implementor ref="hello"></jaxws:implementor>
    </jaxws:endpoint>  
</beans>  

然后访问 http://localhost:8082/testWebservice/cxf/HelloWorld?wsdl (我是用tomcat启动的,接口为8082)

           ![结果如下:][1]

至于怎么调用,在前一个学习笔记中已经说到了 可以通过wsdl2java来实现 地址:http://my.oschina.net/lijie531/blog/343380
我也是在学习阶段,笔记不好之处还望别喷

你可能感兴趣的:(webservice学习之cxf与spring的整合)