1. 新建一个Maven项目,选择webapp模板,命名为WS_Spring_CXF_Tomcat
2. 在POM.xml中添加Spring和CXF的依赖
org.springframework
spring-core
4.1.7.RELEASE
org.springframework
spring-beans
4.1.7.RELEASE
org.springframework
spring-tx
4.1.7.RELEASE
org.springframework
spring-context
4.1.7.RELEASE
org.springframework
spring-context-support
4.1.7.RELEASE
org.springframework
spring-web
4.1.7.RELEASE
org.springframework
spring-webmvc
4.1.7.RELEASE
org.springframework
spring-aop
4.1.7.RELEASE
org.springframework
spring-aspects
4.1.7.RELEASE
org.springframework
spring-jdbc
4.1.7.RELEASE
org.apache.cxf
cxf-core
3.1.5
org.apache.cxf
cxf-rt-frontend-jaxws
3.1.5
org.apache.cxf
cxf-rt-transports-http
3.1.5
3. 修改 project->src->main->web app->WEB-INFO->web.xml
指定spring配置和servlet配置:
WS_Spring_CXF_Tomcat
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
CXFServlet
/webservice/*
4. 添加Spring配置文件到classpath路径下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
注意添加:
1.Spring和CXF名空间;
2.CXF配置文件
5.定义WebService接口及实现类
package com.example.tuo.webservice.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.example.tuo.webservice.HelloWorld;
@Component("helloWorld")
@WebService
public class HelloWorldImpl implements HelloWorld{
public String sayHello(String sb) {
// TODO Auto-generated method stub
return "Hello world," +sb;
}
}
在实现类中添加 "@Component("helloWorld")" 注解,并且修改Spring配置文件(applicationContext.xml),利用Spring管理实现类
6.修改Spring配置文件,暴露微博service的endpoint
implementor="#helloWorld"
address="/HelloWorld">
7. 启动Tomcat,在浏览器中输入webservice的address
http://localhost:8080/WS_Spring_CXF_Tomcat/webservice/HelloWorld?wsdl
OK, 这样我们就成功的讲webservice发布到了Tomcat上。 :)