最近在使用cxf时发现,
本文主要参考cxf使用手册
http://cxf.apache.org/docs/writing-a-service-with-spring.html
- Set up your build for CXF
- Writing a simple JAX-WS service
- Set up the HTTP transport
第一步:设置你的cxf的classpath
1、依赖库
commons-logging-1.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.7.1.jar (or Sun's JavaMail jar)
geronimo-servlet_3.0_spec-1.0.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.13.jar
jaxws-api-2.1.jar
neethi-3.0.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
stax2-api-3.1.1.jar
wsdl4j-1.6.2.jar
woodstox-core-asl-4.0.8.jar
xmlschema-core-2.0.jar
xml-resolver-1.2.jar
2、The Spring jars:
aopalliance-1.0.jar
spring-core-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
3、And the CXF jar:
cxf-2.4.0.jar
第二步 编写你的service
1、代码编写
接口:
package demo.spring.service;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
接口实现:
package demo.spring.service;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}
2、spring文件配置
Lets create a "cxf-servlet.xml" file in our WEB-INF
directory which declares an endpoint bean。cxf-servlet.xml默认在WEB-INF directory但是可以在web.xml里面指定地址。
<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-servlet.xml" />
<jaxws:endpoint
id="helloWorld"
implementor="demo.spring.service.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".
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 see JAX-WS
Configuration.
第三步设置cxf的servlet
Since
we're relying on the default "cxf-servlet.xml" file the default
web.xml referenced by many samples can be
used.
Alternatively,
for arbitrarily named configuration files such as beans.xml,
application-context.xml, etc. we can add the following elements:
- the
Spring ContextLoaderLister. This starts Spring and explicitly loads
the configuration file. We can specify where our file is via
a context-param element.
An example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-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>
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 see JAX-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.service.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.service.HelloWorld"/>
<property name="address" value="http://localhost:9002/services/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
HelloWorld 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/main/java/demo/spring/client/Client.java
分享到:
相关推荐
【标签】"CXF+spring WebService CXF"强调了这些组件的集成,特别是CXF作为Web服务的主要提供者,以及与Spring的紧密配合。CXF不仅可以用作服务提供者,还可以作为客户端来消费其他服务,这在Spring的管理下变得更加...
当我们谈论"CXF3.0.9+SPRING开发webservice例子"时,这意味着我们将探讨如何结合这两个强大的工具来创建和消费Web服务。 首先,让我们深入了解CXF。Apache CXF是基于Java的,它支持多种Web服务标准,如SOAP、...
CXF Restful + Spring3 + Mybatis 后台开发环境搭建
总的来说,"cxf2.7+spring4"整合涉及到的主要知识点包括:Apache CXF的Web服务创建和发布、Spring的依赖注入、Spring与CXF的协同配置、以及注解驱动的开发模式。这样的整合有助于简化Web服务的开发和管理,使得在...
总结来说,Apache CXF 2与Spring 2.5的集成使得Web服务的开发变得更加便捷和灵活。通过Spring的配置,我们可以方便地管理服务的生命周期,同时利用CXF的强大功能来处理各种Web服务协议。这不仅提高了开发效率,也...
在"**cxf+spring+client**"的场景中,我们通常会创建一个Spring配置文件,定义CXF客户端的bean。这个bean将包含服务的地址、端点接口、以及可能需要的安全或认证配置。Spring容器管理CXF客户端,确保在需要时能正确...
简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar...
【标题】"cxfserver+spring+mybatis" 指的是一个基于Apache CXF服务端、Spring框架和MyBatis持久层技术构建的应用。这个组合常见于开发RESTful API或者SOAP Web服务,同时利用Spring进行依赖注入和管理,MyBatis作为...
总结来说,"CXF2.6.4+Spring3.1.1+Jetty7.5.4" 的组合提供了一种高效的Web服务开发和部署方案。Apache CXF处理服务的创建和交互,Spring管理应用的依赖和生命周期,而Jetty则作为一个轻量级的服务器承载这些服务。...
本篇文章将深入探讨如何在CXF3和Spring4的环境中,通过配置文件发布Web服务。 首先,CXF(CXF - The Complete JAXWS/JAXRS Stack)是一个开源的Java框架,用于构建和开发服务导向架构(SOA)的应用程序。它支持多种...
在本文中,我们将深入探讨如何使用Apache CXF 2与Spring 2.5框架来开发Web服务实例。Apache CXF是一个流行的开源项目,它提供了一种简单且强大的方式来实现和消费SOAP和RESTful Web服务。Spring框架则以其模块化、...
web项目使用spring和cxf的一个开发实例,有简单的代码样例和jar。是一个完整的项目,最终发布完成时访问 http://ip:port/项目名称/webservices/ 就会发现你发布的webservice服务。
spring配置文件(可能为applicationContext.xml或cxf-servlet.xml)用于配置CXF和Spring的集成,包括服务发布、WSSecurity的相关配置。 3. **测试文件**:JUnit或其他测试框架的测试类,用于验证WSSecurity功能的...
在IT行业中,构建一个高效且灵活的企业级应用是至关重要的,而"CXF Webservice+Spring+MyBatis 整合"就是一个这样的解决方案。这个整合利用了三个强大的开源框架:Apache CXF、Spring以及MyBatis,来实现Web服务、...
以上是CXF+Spring实现Web Service的基本流程和关键知识点。实际应用中,还需要根据具体的需求和环境进行适当的调整和扩展。例如,如果涉及到大型分布式系统,可能还需要考虑服务治理、负载均衡等问题。通过熟练掌握...
3. **配置Spring**:编写Spring的配置文件,如`applicationContext.xml`,声明 CXF 的服务端点(SEI,Service Endpoint Interface)和实现,以及Struts2的Action类。 4. **配置CXF**:在`cxf-servlet.xml`或类似的...
【标题】"cxf+spring+tomcat"的组合是一个常见的Web服务开发环境,它将Apache CXF(一个用于构建和消费Web服务的开源框架)与Spring框架(一个广泛使用的Java企业级应用开发框架)以及Tomcat(一个流行的轻量级Java...
【CXF+SPRING例子】是一个关于如何将Apache CXF与Spring框架整合的示例项目。Apache CXF是一个开源服务框架,它允许开发者创建和消费Web服务,而Spring框架则是Java应用开发的强大支撑,提供了依赖注入、AOP(面向切...
在"springmvc_cxf.rar"文件中,可能包含了已配置好的Spring MVC项目,包括Spring和CXF的配置文件、服务接口和实现、以及Maven的配置信息。"cxfclinet.rar"则可能包含了客户端的相关代码,如生成的代理类和调用示例。...
这是一个重要的技术组合,因为它允许开发人员利用Spring的依赖注入和管理能力,以及CXF的强大Web服务支持。下面我们将详细讲解这个集成过程的关键步骤和相关知识点。 1. **Apache CXF简介**: Apache CXF是一个...