最近在使用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
分享到:
相关推荐
总的来说,CXF2.5.2是一个强大的Web服务开发框架,它为开发者提供了灵活且全面的工具,帮助他们构建高质量的、符合行业标准的Web服务。无论你是新手还是经验丰富的开发者,理解并掌握CXF的使用将极大地提升你的工作...
在使用"apache-cxf-2.5.2"时,开发者通常会首先阅读文档,了解如何配置和启动CXF服务器,然后使用提供的示例或自己编写代码来创建和部署Web服务。此外,他们还需要了解如何在Spring框架中集成CXF,以便更好地管理...
总的来说,CXF 2.5.2作为一个稳定的版本,为开发者提供了强大的Web服务开发工具,同时与Spring框架的良好集成使得它在企业级应用中得到了广泛的应用。虽然现在已经有了更高级的版本,但对于某些项目来说,2.5.2仍然...
为了确保能够顺利进行WebService接口的开发工作,首先需要确保项目中已经正确配置了CXF的相关依赖。以下为推荐的Maven依赖配置: ```xml <groupId>org.apache.cxf <artifactId>cxf <version>2.5.2 <type>pom...
- 部署服务:使用CXF的Spring配置或Java代码将服务绑定到特定端点。 4. **CXF客户端调用**: - 服务代理生成:CXF提供工具或者API根据WSDL生成服务代理,使得客户端可以像调用本地方法一样调用远程服务。 - 调用...
6. **插件支持**:CXF与Maven、Ant等构建工具良好集成,还支持Spring框架,方便在Spring应用中配置和使用Web服务。 7. **动态客户端**:CXF的动态客户端API允许在运行时通过WSDL生成客户端代码,无需预先编写客户端...
你可以访问Apache官方网站(http://www.apache.org/dist/cxf/)获取最新版本的CXF,例如这里提到的apache-cxf-2.5.2.zip。下载完成后,解压缩文件并将lib目录下的所有JAR包复制到Web项目的WebRoot/WEB-INF/lib目录下...
描述中提到的"还有很多常用的包"表明这个集成包不仅仅是Spring、Struts和iBATIS的基础组件,还包含了其他辅助库,如Groovy、CXF、Docx4j、Guava等。这些库在不同的场景下提供额外的功能支持。 1. Groovy-all-2.3.6....