cxf是webservice的框架,能够和spring无缝整合
##服务端编写
1.创建动态web项目
2.导入cxf和spring相关jar包(cxf核心包:cxf-2.4.2.jar)
3.在web.xml中配置cxf框架的核心servlet
1 <servlet> 2 <servlet-name>cxf</servlet-name> 3 <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> 4 <init-param> 5 <param-name>config-location</param-name> 6 <param-value>classpath:applicationcontext.xml</param-value> 7 </init-param> 8 </servlet> 9 <servlet-mapping>10 <servlet-name>cxf</servlet-name>11 <url-pattern>/webservice/*</url-pattern>12 </servlet-mapping>
4.提供spring框架的配置文件applicationcontext.xml
applicationcontext.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"xmlns:soap="http://cxf.apache.org/bindings/soap"xsi:schemalocation="http://www.springframework.org/schema/beans
">
5.开发一个服务类
注:服务类必须加注解 @webservice
6.在spring中配置文件中注册服务
<jaxws:endpoint id="" address="/hello" implementor=""></jaxws:endpoint><!-- id为服务的id,任意填写 address 为访问地址 implementor为服务类的全类名-->
启动web工程,浏览器访问
###客户端
(用wsdl2java命令生成本地代码调用)
1,在wsdl2java.bat命令所在的文件夹下打开命令窗口,输入:wsdl2java -d . 路径
(路径为service发布后页面的wsdl的全路径,service访问的路径名加?wsdl),回车后会在当前文件夹下生成文件夹
2.把文件夹复制到项目中
(用spring文件注册代理对象调用)
1.创建项目,可以不是web项目,导入jar包
2.将生成的接口复制到项目中,
3.创建applicationcontext.xml文件中配置代理对象
<jaxws:client id="" address = "" serviceclass =""></jaxws:client><!-- id值随意, adress的值为wsdl的路径值,当不在本机是,须要修改ip serviceclass为接口的全路径-->
4.编写实现类(如下为例子)
public static void main(string[] args) {//创建工厂对象classpathxmlapplicationcontext cts = new classpathxmlapplicationcontext(applicationcontext.xml);
fun1 proxy = (fun1) cts.getbean(myclient);
string string = proxy.sayhello(呵呵, 12);
system.out.println(string);
}
以上就是spring整合cxf框架实例教程的详细内容。