`
icarusliu
  • 浏览: 237209 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

cxf:Writing a service with Spring

阅读更多

This example will lead you through creating your first service with Spring. You'll learn how to:

  • Set up your build for CXF
  • Writing a simple JAX-WS service
  • Set up the HTTP transport

This example corresponds to the spring_http example in the CXF distribution.

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) {
        return "Hello " + text;
    }
}

The @WebService annotation 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:

  • id specifies the id of the bean in the Spring context.
  • implementor specifies the implementation class.
  • address specifies 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".

Setting up the Servlet

We'll need to add two things to our web.xml file:

  1. the Spring ContextLoaderLister. This starts Spring and loads our beans.xml file. We can specify where our file is via a context-param element.
  2. 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

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-2.0.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");

client code at
http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/demo/spring/client/Client.java

Advanced Steps

For more information on using Spring you may want to read the Configuration and Spring sections of the User's Guide.

分享到:
评论

相关推荐

    cxf +spring

    `&lt;bean&gt;`定义了服务实现,`&lt;cxf:service&gt;`则声明了服务接口和地址。 在实际开发中,【压缩包子文件的文件名称列表】"CXF"可能包含CXF的示例代码、配置文件、或者是一些预打包的服务。这些资源可以帮助初学者了解和...

    CXF源码:CXF_Spring源码

    - **客户端配置**:在`CXFSpringClient`目录中,可能包含客户端如何调用CXF服务的代码,可能通过JAX-WS的`Service`类或者使用Spring的`WebServiceTemplate`。 3. **服务发布与消费** 在Spring环境中,CXF服务...

    apache-cxf-2.4.6.zip

    Apache CXF是一个开源的Java框架,它主要用于构建和开发服务导向架构(SOA)和Web服务。这个"apache-cxf-2.4.6.zip"压缩包包含了CXF框架的2.4.6版本,这是一个相对早期的版本,发布于2012年。在深入探讨CXF之前,...

    CXF和Spring整合开发的服务端及客户端

    CXF(CXF: Composite eXtensible Services Framework)是一款开源的Java框架,主要用于构建和开发服务导向架构(SOA)中的Web服务。它支持多种协议和标准,如SOAP、RESTful、JAX-RS和JAX-WS等。Spring框架则是Java...

    使用CXF: Java 2 WSDL

    【标题】:“使用CXF:Java 2 WSDL” 【描述】:在Java开发中,Apache CXF是一个广泛使用的开源框架,它允许开发者构建和部署Web服务。"Java 2 WSDL"指的是从Java类生成WSDL(Web Services Description Language)...

    13.为CXF与Spring整合发布WebService添加拦截器进行权限控制

    &lt;/cxf:service&gt; ``` 在`AuthorizationInterceptor`中,我们可以在`aroundInvoke`方法中实现具体的权限验证逻辑。例如,我们可以检查请求头中的认证令牌,或者从Spring Security中获取当前登录的用户信息,判断...

    CXF关于Aegis的简单示例

    在这个配置中,`cxf:jaxws-service`元素定义了服务,`serviceClass`属性指定服务接口,`wsdlLocation`指向WSDL文件的位置。`cxf:dataBinding`元素用于设置数据绑定为Aegis。`myServiceInterface`和`myServiceImpl`...

    web service cxf 2.7.5 与spring 3.0 集成

    &lt;cxf:properties&gt; &lt;entry key="org.apache.cxf.logging.FaultListener" value-ref="faultListener"/&gt; &lt;/cxf:properties&gt; &lt;/cxf:bus&gt; &lt;jaxws:endpoint id="myWebService" implementor="#myWebServiceImpl...

    cxf整合spring

    在IT行业中,Web服务是应用程序之间进行通信的一种标准方法,而CXF和Spring都是Java生态系统中的关键组件。本文将深入探讨如何将CXF与Spring框架整合,以构建高效且灵活的Web服务解决方案。 首先,让我们了解CXF。...

    基于maven的cxf+spring简单demo

    &lt;/cxf:features&gt; &lt;/cxf:bus&gt; &lt;jaxws:endpoint id="helloService" implementor="#helloServiceImpl" address="/hello"/&gt; ``` 最后,通过Maven的cxf-codegen-plugin插件生成客户端和服务端的Stubs,并运行项目...

    cxf-2.7.3与spring3整合开发步骤.

    在本文中,我们将深入探讨如何将Apache CXF 2.7.3与Spring 3.0.7框架整合进行开发。Apache CXF是一个开源的Java框架,主要用于构建和部署SOAP和RESTful Web服务,而Spring则是一个广泛使用的应用框架,提供了依赖...

    CXF整合spring同时支持JSON和XML配置的HelloWorld

    在上面的配置中,`&lt;cxf:features&gt;`元素启用了JSON支持,`&lt;cxf:jsr311/&gt;`则支持JAX-RS标准,使得CXF可以处理RESTful请求。`&lt;jaxws:endpoint&gt;`定义了服务的实现类和访问地址。 然后,创建一个Java类(如`...

    CXF和Spring整合,并且添加拦截器

    &lt;cxf:endpoint id="myService" address="/myService" serviceClass="com.example.MyService" xmlns:ns="http://example.com/service"&gt; &lt;!-- 配置服务相关的属性 --&gt; &lt;/cxf:endpoint&gt; ``` 3. **定义服务接口和...

    CXF Spring Web Service 程序

    【CXF与Spring整合Web服务详解】 在Java世界中,Apache CXF是一个广泛使用的开源框架,用于构建和部署Web服务。它提供了丰富的功能,包括SOAP、RESTful API的支持,以及与Spring框架的深度集成。本篇文章将深入探讨...

    CXF3.0.9+SPRING开发webservice例子

    CXF(CXF: Apache CXF)是一个开源的Java框架,专为构建和部署Web服务而设计。Spring框架则是Java企业级应用开发的事实标准,提供了一个全面的编程和配置模型,使得开发、测试和集成复杂的应用变得简单。当我们谈论...

    CXF2+Spring2.5开发WebService实例

    &lt;/cxf:features&gt; &lt;/cxf:bus&gt; &lt;jaxws:endpoint id="helloWorld" implementor="#helloWorldImpl" address="/HelloWorld"/&gt; ``` 在这里,`implementor`属性指定了Web服务的实现类,`address`属性定义了服务的...

    CXF和spring整合所需jar包

    在Java企业级开发中,Apache CXF和Spring框架的整合是常见的实践,它们结合可以创建高效、灵活的服务提供和消费平台。Apache CXF是一个开源的Web服务框架,它支持多种Web服务标准,如SOAP、RESTful等。而Spring框架...

    spring3.0整合cxf3.1.4所需的最小jar包

    &lt;cxf:features&gt; &lt;cxf:logging/&gt; &lt;/cxf:features&gt; &lt;/cxf:bus&gt; &lt;bean id="myService" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"&gt; &lt;property name="serviceClass" value=...

    CXF与Spring整合以及所需jar

    &lt;cxf:service beanId="helloWorldImpl" id="helloWorldService"&gt; &lt;cxf:interface namespaceURI="http://example.com/helloworld" serviceName="HelloWorldService"/&gt; &lt;/cxf:service&gt; ...

    spring与cxf整合开发

    服务器端被调用的类上要加注解@WebService,否则访问http://localhost:8080/cxf_spring_service/mm/cxf?wsdl时看不到方法和参数 2.在cmd中输入wsdl2java http://localhost:8080/cxf_spring_service/mm/cxf?wsdl报...

Global site tag (gtag.js) - Google Analytics