|
官方下载:http://cxf.apache.org/ 最新稳定版本是2.2.6
1. 概述
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。
2. 与spring 集成
2.1 定义服务端接口
package com.alex.cxf;
import javax.jws.WebService;
@WebService
public interface IVote {
public String vote(String username,int point);
}
接口实现类:
package com.alex.cxf;
public class Vote implements IVote{
private String excludeName="alex";
public String vote(String username, int point) {
String result="";
if(excludeName.equals(username)){
result="不能重复投票";
}else{
result="O(∩_∩)O谢谢投票";
}
return result;
}
}
获取上下文bean的Util类:
package com.alex.cxf;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class BeanUtil implements ApplicationContextAware {
private static ApplicationContext staticContext;
public void setApplicationContext(ApplicationContext context)
throws BeansException {
staticContext = context;
}
public static Object getBean(String strBeanName) {
return staticContext.getBean(strBeanName);
}
}
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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="vote" implementor="com.alex.cxf.Vote"
address="/Vote" />
</beans>
2.2 客户端
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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:client id="clientvote" serviceClass="com.alex.cxf.IVote"
address="http://localhost:8080/cxf/services/Vote" />
</beans>
jsp调用:
<%@ page language="java"
import="java.util.*,org.springframework.context.*,com.alex.cxf.*"
pageEncoding="GBK"%>
<html>
<body>
<%
IVote client=(IVote) BeanUtil.getBean("clientvote");
System.out.println(client.vote("jack",100));
%>
</body>
</html> |
|
|