- 浏览: 52682 次
- 性别:
- 来自: 武汉
最新评论
-
zhangdong1986:
great article, the content cove ...
利用eclipse编写高质量的java代码
Setting up your build
Open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory.
commons-logging-1.1.jar geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar) geronimo-annotation_1.0_spec-1.1.jar (JSR 250) geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar) geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar) geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181) jaxb-api-2.0.jar jaxb-impl-2.0.5.jar jaxws-api-2.0.jar neethi-2.0.jar saaj-api-1.3.jar saaj-impl-1.3.jar stax-api-1.0.1.jar wsdl4j-1.6.1.jar wstx-asl-3.2.1.jar XmlSchema-1.2.jar xml-resolver-1.2.jar
The Spring jars:
aopalliance-1.0.jar spring-core-2.0.8.jar spring-beans-2.0.8.jar spring-context-2.0.8.jar spring-web-2.0.8.jar
And the CXF jar:
cxf-2.1.jar
Writing your Service
First we'll write our service interface. It will have one operation called "sayHello" which says "Hello" to whoever submits their name.
package demo.spring; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String text); }
Our implementation will then look like this:
package demo.spring; import javax.jws.WebService; @WebService(endpointInterface = "demo.spring.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; } }
The@WebServiceannotation on the implementation class lets CXF know which interface to use when creating WSDL. In this case its simply our HelloWorld interface.
Declaring your server beans
CXF contains support for "nice XML" within Spring 2.0. For the JAX-WS side of things, we have a <jaxws:endpoint> bean which sets up a server side endpoint.
Lets create a "beans.xml" file in our WEB-INF directory which declares an endpoint bean:
<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="helloWorld" implementor="demo.spring.HelloWorldImpl" address="/HelloWorld" /> </beans>
If you want to reference a spring managed-bean, you can write like this:
<bean id="hello" class="demo.spring.HelloWorldImpl" /> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
The bean uses the following properties:
- idspecifies the id of the bean in the Spring context.
- implementorspecifies the implementation class.
- addressspecifies the location the service will be hosted. This should just be a related path. This is because CXF can't know the war name and the servlet container's listening port, CXF will update the endpoint address with the request url at the runtime.
To provide a bean name instead of a classname as an implementor, simply supply the bean-name prepended with "#", e.g. implementor="#myBean".
You can also do more sophisticated things with the<jaxws:endpoint>element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the service. For more on this seeJAX-WS Configuration.
Setting up the Servlet
We'll need to add two things to our web.xml file:
- the SpringContextLoaderLister. This starts Spring and loads our beans.xml file. We can specify where our file is via acontext-paramelement.
- the CXF Servlet
<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>
It is important to note that the address that you chose for your endpoint bean must be one your servlet listens on. For instance, if my Servlet was register for "/some-services/*" but my address was "/more-services/HelloWorld", there is no way CXF could receive a request.
Create a Client (Easy Way)
Just like the<jaxws:endpoint>used on the server side, there is a<jaxws:client>that can be used on the client side. You'll give it a bean name, the service interface, and the service URL, and it will create a bean with the specified name, implementing the service interface, and invoking the remote SOAP service under the covers:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <jaxws:client id="helloClient" serviceClass="demo.spring.HelloWorld" address="http://localhost:9002/HelloWorld" /> </beans>
You can now inject that "helloClient" bean into any other Spring bean, or look it up from the Spring application context manually with code like this:
ApplicationContext context = ...; // your Spring ApplicationContext HellWorld client = (HelloWorld) context.getBean("helloClient");
You can also do more sophisticated things with the<jaxws:client>element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the client. For more on this seeJAX-WS Configuration.
Create a Client (More Manual Way)
CXF includes a JaxWsProxyFactory bean which create a client for you from your service interface. You simply need to tell it what your service class is (the HelloWorld interface in this case) and the URL of your service. You can then create a client bean via the JaxWsProxyFactory bean by calling it's create() method.
Here's an example:
<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/schema/jaxws.xsd"> <bean id="client" class="demo.spring.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="demo.spring.HelloWorld"/> <property name="address" value="http://localhost:9002/HelloWorld"/> </bean> </beans>
If you were going to access your client you could now simply pull it out of the Spring context (or better yet, inject it into your application using Spring!):
ApplicationContext context = ...; // your Spring ApplicationContext HellWorld client = (HelloWorld) context.getBean("client");
Some usage scenarios will require more extensive configuration (and this is not the case with the<jaxws:client>syntax described above). For more information, seeJAX-WS Configuration. |
发表评论
-
JVM最大分配内存
2011-08-27 16:28 935分析了当前比较流行的 ... -
apache+tomcat 负载均衡
2011-07-15 17:10 797其实无论是分布式,数据缓存,还是负载均衡,无非就是改善网站的性 ... -
关于java文件编译后,同一个文件出现"$"的class
2011-06-08 15:00 1285在java中,如果在一个类中定义了内部类,刚会生成: supe ... -
jdbc操作测试类
2011-05-31 20:05 759package com.fs.test; import ... -
Spring中JdbcTemplate类query的使用例子
2011-05-12 21:01 1149/** 使用三种Callback接口 ... -
Properties操作例子类
2011-04-19 23:48 799import java.io.FileInputStream; ... -
Java中解决所有路径问题
2011-03-19 02:30 554Java中使用的路径,分为两种:绝对路径和相对路径。归 ... -
利用eclipse编写高质量的java代码
2011-03-19 01:51 853敏捷开发的理念已经 ... -
spring下连接池比较
2010-01-18 11:11 877最近遇到了一个奇怪的问题,使用了Apache的连接池,当数据库 ... -
java遍历文件夹
2010-07-16 17:05 629A.不使用递归:import java.io.File;imp ... -
jvm学习
2010-07-16 17:07 644由于最近时间充足, ... -
servlet页面跳转
2010-07-19 11:45 615一直对Servlet的几种页面 ... -
tomcat内存溢出
2010-08-05 15:17 691这几天,在服务器上增加了一个host,但自从增加了这个host ... -
tomcat定时重启
2010-08-20 09:06 1115保存以下脚本成tomcat.bat,计划任务指定每天4点执行该 ... -
struts2+json+jquery 级联查询
2010-12-09 15:47 1329js代码 $(document).ready( funct ...
相关推荐
4. `pom.xml`:Maven构建文件,描述了项目的依赖和构建过程。 通过研究源码,开发者可以深入理解CXF如何将Java类映射到WSDL服务,如何处理WS-Security安全策略,以及如何使用不同的传输机制(如HTTP、JMS等)。此外...
Apache CXF是一个开源的服务框架,用于构建和开发服务端和客户端应用程序,支持多种Web服务标准,如SOAP、RESTful API等。 【描述】中的"基于cxf webservice的加密"指出了这个项目是围绕CXF创建的Web服务,并且该...
2.1 CXF开发例子 2.1.1 步骤图 开发一个CXF Web服务通常包括以下几个步骤: 1. 创建服务接口和实现类。 2. 定义WSDL文件或从接口自动生成。 3. 配置CXF服务端点。 4. 部署并启动服务。 5. 创建客户端并调用服务。 ...
- **代码生成**:CXF提供代码生成工具,可以从WSDL生成服务端和客户端的Java代码,极大地简化了开发过程。 **1.4 CXF环境要求** 在使用CXF前,需要确保已安装了Java Development Kit(JDK)以及Apache CXF的依赖库...
Spring作为一个全面的轻量级应用框架,提供了强大的依赖注入、AOP(面向切面编程)等功能,而CXF则是一个优秀的服务端和客户端Web服务实现框架,支持多种Web服务标准,如SOAP、RESTful等。本文将详细介绍如何将...
##### 2.1 下载 Apache CXF 访问 Apache CXF 官方网站 [http://cxf.apache.org/](http://cxf.apache.org/) 并下载最新版本的 CXF。假设下载的是 `apache-cxf-2.7.10.zip`。 ##### 2.2 解压缩 将下载好的 ZIP 包...
CXF不仅提供了一个强大的工具集,用于快速开发服务端和客户端,还支持多种编程模型,如JAX-WS(Java API for XML Web Services)和JAX-RS(Java API for RESTful Web Services)。 **1.1 CXF 支持** - **协议标准*...
Apache CXF是一个开源框架,用于构建和开发Web服务。它支持多种协议和技术,包括SOAP、XML、REST等,使得开发者能够轻松地创建高性能的服务端应用。本书《Apache CXF Web Service Development》为读者提供了全面深入...
- 分布式应用:适用于构建跨网络、跨平台的应用程序和服务。 #### 二、WebService 的开源实现 - **简介**:虽然 WebService 是一种标准而非特定技术,但不同平台和语言提供了各自的实现方案。在 Java 领域,有...
在实际应用中,JAX-WS通常与Java EE服务器(如Tomcat、JBoss或WebLogic)一起使用,或者在Java SE环境中通过Apache CXF、Metro等实现库来提供和使用Web服务。开发者可以利用JAX-WS的强大功能,轻松地构建符合SOA...
Apache CXF是一个强大的开源框架,用于构建和消费基于SOAP和REST风格的WebService。它不仅支持JAX-WS规范,还支持JAX-RS(Java API for RESTful Web Services),这使其能够同时满足传统SOAP服务和现代RESTful服务的...
**CXF**(**C**eltix **X**Fire **F**usion)是一个高性能、可扩展且易于使用的开源框架,用于构建和消费Web Services。它是Apache软件基金会的一个顶级项目,由ObjectWeb Celtix和Codehaus XFire两个项目合并而成。...
Apache CXF 是一个开源的Java框架,用于构建和开发服务导向架构(SOA)的应用和服务。CXF使得开发者能够轻松地创建和部署Web服务,同时支持多种协议和规范,包括SOAP、RESTful、JAX-RS和JAX-WS等。在本文中,我们将...
JAX-WS 使用 JAXB 2.0 提供数据绑定服务,并支持通过定制来控制生成的服务端点接口。通过对注解的支持,JAX-WS 进一步简化了 Web 服务的开发过程,并缩小了运行时 JAR 文件的大小。 ##### 2.3 Apache CXF Apache ...
- **客户端生成**:使用WSDL文件自动生成客户端代码,简化了客户端与服务端之间的交互过程,提高了开发效率。 - **头信息处理**:服务端可以解析SOAP头中的信息,例如NoSOAPAction字段,以便正确处理请求或返回特定...
2.1 **基础页面组件**:前端页面通常基于jQuery框架,如easyui、jquery ui或dwz,用于构建页面元素如图表、编辑器、表单验证、listbox、日期组件、进度条、对话框、按钮、Grid、Tree、Menu、Tab和Form等。...