通过前面两节的讲解,相信你对CXF框架开始有一些认识了。在当今项目开发中,Spring框架基上都用到过,那么它怎么与CXF结合呢,这就是我们这一间要讲的内容。好了,闲话少说。
首先,在前面基础上再导入几个spring要用到的几个.jar包:
1、spring-asm.jar
2、spring-beans.jar
3、spring-context.jar
4、spring-core.jar
5、spring-expression.jar
6、spring-aop.jar
7、spring-web.jar
第一步:新建一个服务端web project,导入要用到的cxf和spring的.jar包,修改web.xml。配置如下
第五步:新建一个客户端web project,导入要用到的cxf和spring的.jar包
第六步:将服务端的接口类及JavaBean对象类copy到客户端工程中,且路径要与服务端路径一致
第七步:新建一个spring Bean的xml配置文件,配置CXF webservices的客户端
首先,在前面基础上再导入几个spring要用到的几个.jar包:
1、spring-asm.jar
2、spring-beans.jar
3、spring-context.jar
4、spring-core.jar
5、spring-expression.jar
6、spring-aop.jar
7、spring-web.jar
第一步:新建一个服务端web project,导入要用到的cxf和spring的.jar包,修改web.xml。配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Spring 容器加载的配置文件 设置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext-server.xml
</param-value>
</context-param>
<!-- Spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- WebServices设置 -->
<servlet>
<servlet-name>CXFServices</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServices</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第二步:新建一个接口类和接口实现类
package com.ms.services;
import java.util.List;
import javax.jws.WebService;
import com.ms.model.UserInfo;
@WebService
public interface IHelloServices {
public String sayHello(String name);
public String sayHelloToAll(List<UserInfo> users);
}
package com.ms.services.impl;
import java.util.List;
import javax.jws.WebService;
import com.ms.model.UserInfo;
import com.ms.services.IHelloServices;
@WebService(endpointInterface="com.ms.services.IHelloServices")
public class HelloServicesImpl implements IHelloServices {
public String sayHello(String name) {
return "Hello "+name+" .";
}
public String sayHelloToAll(List<UserInfo> users) {
String hello = "hello ";
for(UserInfo user:users){
hello += user.getUserName()+" ,";
}
hello += " ,everybody.";
return hello;
}
}
第三步:新建一个spring Bean的xml文件,配置CXF webservices的服务
<?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-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import apache CXF bean definition 固定-->
<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" />
<!-- services接口配置 -->
<bean id="helloServicesBean" class="com.ms.services.impl.HelloServicesImpl" />
<!-- CXF 配置WebServices的服务名及访问地址 -->
<jaxws:server id="helloServices" address="/HelloServices"
serviceClass="com.ms.services.IHelloServices">
<jaxws:serviceBean>
<ref bean="helloServicesBean"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
第四步:将工程部署到Tomcat中运行,在IE中输入"
http://localhost:8090/CxfServer_Spring/services
",测试服务是否发布成功第五步:新建一个客户端web project,导入要用到的cxf和spring的.jar包
第六步:将服务端的接口类及JavaBean对象类copy到客户端工程中,且路径要与服务端路径一致
第七步:新建一个spring Bean的xml配置文件,配置CXF webservices的客户端
<?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-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import apache CXF bean definition -->
<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" />
<!-- CXF webservices 客户端配置 -->
<jaxws:client id="helloClient" serviceClass="com.ms.services.IHelloServices"
address="http://localhost:8090/CxfServer_Spring/services/HelloServices">
</jaxws:client>
</beans>
第八步:新建一个测试类进行测试,代码如下
package com.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ms.model.UserInfo;
import com.ms.services.IHelloServices;
public class Client {
public static void main(String[] args) {
invokeBySpring();
}
/**
* 通过Spring测试webservices
*/
public static void invokeBySpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
IHelloServices helloServices = context.getBean("helloClient",IHelloServices.class);
List<UserInfo> users = new ArrayList<UserInfo>();
users.add(new UserInfo("vicky",23));
users.add(new UserInfo("caty",23));
users.add(new UserInfo("ivy",23));
users.add(new UserInfo("kelly",23));
String helloAll = helloServices.sayHelloToAll(users);
System.out.println(helloAll);
}
public static void invoke(){
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(IHelloServices.class);
//设置WebService地址
factory.setAddress("http://localhost:8090/CxfServer_Spring/services/HelloServices");
IHelloServices helloServices = (IHelloServices)factory.create();
System.out.println("invoke helloServices webservice...");
String hello = helloServices.sayHello("vicky");
System.out.println(hello);
}
}