为了入门不停地google也未找到好的入门资料,经过几天的苦苦摸索,好不容易才调试成功。为了让新加入CXF阵营的网友少走
弯路,整理一下供大家参考。鉴于于对CXF、spring还不甚了解,还望老手们能够补充、指正。
apache-cxf-2.1.1 下载地址:http://cxf.apache.org
tomcat 6.0.18 下载地址:http://tomcat.apache.org/
ant 1.7.1 下载地址:http://ant.apache.org/
eclipse 3.4.0 下载地址:http://www.eclipse.org
配置环境变量
ANT_HOME=C:/apache-ant-1.7.1
CATALINA_HOME=c:/apache-ant-1.7.1
CXF_HOME=c:/apache-cxf-2.1.1
CLASSPATH=.;%JAVA_HOME%/lib/tools.jar
PATH=....在末尾增加;%JAVA_HOME%/bin;%ANT_HOME%/bin;
在eclipse新建一个java project,假设路径 d:/project/cxf/CXFDemo
目录结构
src
|—demo
|__spring
|__ AccountDeposit.java
|__ AccountDepositImpl.java
|__ client
|__ Client.java
|__ client-beans.xml
build
|__自动生成
webapp
|__web.xml
|__beans.xml
common-build.xml // 此文件直接从apache-cxf-2.1.1/samples目录下拷贝过来
build.xml // 用于ant编译、打包,也是从cxf例子中拷贝过来略作修改
在命令提示符下
cd d:/project/cxf/CXFDemo
ant
ant war
这个时候会在D:/project/cxf/CXFDemo/build/war目录生成cxfdemo.war
把cxfdemo.war拷贝到C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps目录下面,这样发布服务就完成。
访问 http://localhost:8080/cxfdemo/AccountDeposit?wsdl 如果能够看到接口信息,那说明服务已经发布成功了。
代码清单:
AccountDeposit.java
- package demo.spring;
- import javax.jws.WebService;
- @WebService
- public interface AccountDeposit {
- /**
- * 计算帐户日均存款
- *
- * @param acctNo
- * @param fromDate
- * @param toDate
- * @return
- */
- double avg(String acctNo, String fromDate, String toDate);
- }
AccountDepositImpl.java
- package demo.spring;
- import javax.jws.WebService;
- @WebService(endpointInterface = "demo.spring.AccountDeposit")
- public class AccountDepositImpl implements AccountDeposit {
- private static Log log = LogFactory.getLog(AccountDepositImpl.class);
- @Override
- public double avg(String acctNo, String fromDate, String toDate) {
- // 省略
- return 1.0;
- }
- }
web.xml
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <!-- START SNIPPET: webxml -->
- <web-app>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <display-name>CXF Servlet</display-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>
- </web-app>
- <!-- END SNIPPET: webxml -->
beans.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- START SNIPPET: beans -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <jaxws:endpoint
- id="accountDeposit"
- implementor="demo.spring.AccountDepositImpl"
- address="/AccountDeposit" />
- </beans>
- <!-- END SNIPPET: beans -->
build.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="Spring HTTP Servlet demo" default="build" basedir=".">
- <import file="./common_build.xml"/>
- <property name="cxf.war.file.name" value="cxfdemo"/>
- <target name="war" depends="build">
- <cxfwar filename="${cxf.war.file.name}.war" webxml="webapp/WEB-INF/web.xml" />
- </target>
- </project>
Client.java
- package demo.spring.client;
- import demo.spring.AccountDeposit;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public final class Client {
- private Client() {
- }
- public static void main(String args[]) throws Exception {
- // START SNIPPET: client
- ClassPathXmlApplicationContext context
- = new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});
- AccountDeposit client = (AccountDeposit)context.getBean("client");
- double response = client.avg("940200002595585", "20080101", "20080331");
- System.out.println("Response: " + response);
- System.exit(0);
- // END SNIPPET: client
- }
- }
client-beans.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- START SNIPPET: beans -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
- <bean id="client" class="demo.spring.AccountDeposit"
- factory-bean="clientFactory" factory-method="create"/>
- <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
- <property name="serviceClass" value="demo.spring.AccountDeposit"/>
- <property name="address" value="http://localhost:8080/cxfdemo/AccountDeposit"/>
- </bean>
- </beans>
- <!-- END SNIPPET: beans -->