CXF入门

最近CXF新手上路,apache网站上面的CXF user guide看得我一头雾水。(估计CXF项目组都是高手,写得资料也是讳莫高深。:))
为了入门不停地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

  1. package demo.spring;
  2. import javax.jws.WebService;
  3. @WebService
  4. public interface AccountDeposit {
  5.  /**
  6.   * 计算帐户日均存款
  7.   *  
  8.   * @param acctNo
  9.   * @param fromDate
  10.   * @param toDate
  11.   * @return
  12.   */
  13.  double avg(String acctNo, String fromDate, String toDate);
  14. }

 

AccountDepositImpl.java

  1. package demo.spring;
  2. import javax.jws.WebService;
  3. @WebService(endpointInterface = "demo.spring.AccountDeposit")
  4. public class AccountDepositImpl implements AccountDeposit {
  5.  private static Log log = LogFactory.getLog(AccountDepositImpl.class);
  6.  @Override
  7.  public double avg(String acctNo, String fromDate, String toDate) {
  8.   // 省略
  9.   return 1.0;
  10.     }
  11. }

web.xml

 

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE web-app
  3.     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  4.     "http://java.sun.com/dtd/web-app_2_3.dtd">
  5. <!-- START SNIPPET: webxml -->
  6. <web-app>
  7.  <context-param>
  8.   <param-name>contextConfigLocation</param-name>
  9.   <param-value>WEB-INF/beans.xml</param-value>
  10.  </context-param>
  11.  <listener>
  12.   <listener-class>
  13.    org.springframework.web.context.ContextLoaderListener
  14.   </listener-class>
  15.  </listener>
  16.  <servlet>
  17.   <servlet-name>CXFServlet</servlet-name>
  18.   <display-name>CXF Servlet</display-name>
  19.   <servlet-class>
  20.    org.apache.cxf.transport.servlet.CXFServlet
  21.   </servlet-class>
  22.   <load-on-startup>1</load-on-startup>
  23.  </servlet>
  24.  <servlet-mapping>
  25.   <servlet-name>CXFServlet</servlet-name>
  26.   <url-pattern>/*</url-pattern>
  27.  </servlet-mapping>
  28. </web-app>
  29. <!-- END SNIPPET: webxml -->

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- START SNIPPET: beans -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.  xmlns:jaxws="http://cxf.apache.org/jaxws"
  6.  xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  9.  <import resource="classpath:META-INF/cxf/cxf.xml" />
  10.  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  11.  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  12.  <jaxws:endpoint 
  13.    id="accountDeposit" 
  14.    implementor="demo.spring.AccountDepositImpl" 
  15.    address="/AccountDeposit" />
  16.    
  17. </beans>
  18. <!-- END SNIPPET: beans -->

build.xml

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project name="Spring HTTP Servlet demo" default="build" basedir=".">
  3.  <import file="./common_build.xml"/>        
  4.     <property name="cxf.war.file.name" value="cxfdemo"/>
  5.       <target name="war" depends="build">
  6.    <cxfwar filename="${cxf.war.file.name}.war" webxml="webapp/WEB-INF/web.xml" />
  7.     </target>
  8. </project>

Client.java

  1. package demo.spring.client;
  2. import demo.spring.AccountDeposit;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public final class Client {
  5.     private Client() {
  6.     }
  7.     public static void main(String args[]) throws Exception {
  8.         // START SNIPPET: client
  9.         ClassPathXmlApplicationContext context 
  10.             = new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});
  11.         AccountDeposit client = (AccountDeposit)context.getBean("client");
  12.         double response = client.avg("940200002595585""20080101""20080331");
  13.         System.out.println("Response: " + response);
  14.         System.exit(0);
  15.         // END SNIPPET: client
  16.     }
  17. }

client-beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- START SNIPPET: beans -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.  xmlns:jaxws="http://cxf.apache.org/jaxws"
  6.  xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
  9.     <bean id="client" class="demo.spring.AccountDeposit" 
  10.       factory-bean="clientFactory" factory-method="create"/>
  11.     
  12.  <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
  13.    <property name="serviceClass" value="demo.spring.AccountDeposit"/>
  14.    <property name="address" value="http://localhost:8080/cxfdemo/AccountDeposit"/>
  15.  </bean>
  16.    
  17. </beans>
  18. <!-- END SNIPPET: beans -->


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值