`

webService学习之cxf与spring的简单整合

阅读更多
直接上代码吧:

一:先把cxf的jar包导进去,我是直接在官网下载的cxf的包,解压后直接把lib文件夹里的jar一次
   性全部丢进去了,因为在学习,还不知道哪些包要哪些包不要的,所以干脆直接放进去

二:写一个简单的接口

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.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,配置如下
<?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">  
        <!-- 实现接口的bean -->
        <bean id="hello" class="test.wervice.impl.HelloWorldImpl"/>
          
        <!--第一种配置-->
       
        <jaxws:endpoint address="/HelloWorld" >
        	<jaxws:implementor ref="hello" />
        </jaxws:endpoint>  
        
        <!-- 第二种配置方法 -->
   <!--  <jaxws:endpoint address="/HelloWorld" implementor="test.wervice.impl.HelloWorldImpl"/> -->    
      
    </beans>  


最后启动项目,在浏览器端输入address的地址:http://localhost:8082/testWebservice/cxf/HelloWorld?wsdl

得到如下结果:





  • 大小: 148.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics