CXF Spring整合 ——又一个helloword!

1、到Apache下载cxf的最新jar包 本人的是 2.5.2
地址:http://www.apache.org/dyn/closer.cgi?path=/cxf/2.5.2/apache-cxf-2.5.2.zip

2、新建web项目,整合Spring
右键点击工程名称,MyEclipse->Add Spring Capabilities。
选择Spring version为Spring 2.5,选中Spring 2.5 AOP Libraries、Spring 2.5 Core Libraries、Spring 2.5 Persistence Core Libraries、Spring 2.5 Persistence JDBC Libraries、Spring 2.5 Web Libraries共5个包。选中Copy checked Library contents to project folder (TLDs always copied),点击Next。
点击Next,点击Folder文本框后的Browse,在弹出对话框中选择spring配置文件存储位置为WebRoot/WEB-INF,点击Finish。至此已经完成加载Spring,在WEB-INF文件夹下已经生成了配置文件applicationContext.xml。

3、解压apache-cxf-2.5.2包,将所有jar加载到类路径下

4、首先在web.xml中添加如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


5、然后在src目录中,新建一个applicationContext-server.xml文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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" />

</beans>


6、下面开始写服务器端代码,首先定制服务器端的接口,代码如下:

package test;

import javax.jws.WebService;

@WebService
public interface IHelloWorld {
public String sayHello(String name);
}


下面编写WebService的实现类,服务器端实现代码如下:

package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements IHelloWorld {


public String sayHello(String name) {
System.out.println("sayHello is called by " + name);
return "Hello " + name;
}

}


注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。
下面要在applicationContext-server.xml文件中添加如下配置:

<jaxws:endpoint
id="helloWorld"
implementor="test.HelloWorldImpl"
address="/HelloWorld" />

下面启动tomcat服务器后,在WebBrowser中请求:
http://localhost:8088/SnipeCXFServer/HelloWorld?wsdl
如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的HelloWorld就是上面xml配置中的address的名称,是一一对应的。

7、首先增加applicationContext-client.xml配置文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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:client
id="helloWorldClient"
address="http://localhost:8088/SnipeCXFServer/HelloWorld"
serviceClass="test.IHelloWorld"/>
</beans>


8、下面编写客户端请求的代码,代码如下:

package client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.IHelloWorld;

public class Client {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
IHelloWorld helloWorld = (IHelloWorld) context.getBean("helloWorldClient");
System.out.println(helloWorld.sayHello("Test"));
}

}


运行后结果如下:
2012-3-9 15:48:37 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://test/}IHelloWorldService fromclasstest.IHelloWorld
Hello Test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值