在这小例子里我就不多做开发环境之类的说明了(上一例子已经交代清楚了),所以接下来就直接开打了!
新建一个web项目,命名为:CXFServer
建一个包:com.server.dao
在该包下面建一个接口,命名为:Hello,具体代码如下:
package com.server.dao;
import javax.jws.WebService;
@WebService
public interface Hello {
public String hello(String username);
}
新建一个包:com.server.service
在该包下建一个现实类,命名为:HelloImpl具体代码如下:
package com.server.service;
import javax.jws.WebService;
import com.server.dao.Hello;
@WebService
public class HelloImpl implements Hello {
public String hello(String username) {
System.out.println("server is called!");
return "sayHello" + username;
}
}
既然要用到Spring,那么就少不了在web.xml里面配置Spring的过滤器!
在web.xml配置Spring,如下所示:
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>apache cxf 配置 webservice 服务</description>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<listener>
<description>spring 的监听</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<description>spring 的配置文件加载路径</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
</web-app>
项目增加Spring功能后,那么就要配置Spring文件了!
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="service"
implementor="com.server.service.HelloImpl" address="/webserviceHello" />
</beans>
<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" />
这3句是固定的一个配置!
<jaxws:endpoint id="service"
implementor="com.server.service.HelloImpl" address="/webserviceHello" />
id:指在spring配置的bean的ID.
Implementor:指明具体的实现类.
Address:指明这个web service的相对地址
把项目发布到tomcat上,启动tomcat,在浏览器打开http://localhost:8080/CXFServer/services/webserviceHello?wsdl 能现实如下界面,证明服务器已经成功发布了!
有的同学可能会对这个访问地址存在疑问:
http://localhost:8080/CXFServer/services/webserviceHello?wsdl
问什么是这样子的访问地址呢?
http://localhost:8080/CXFServer是本项目的访问地址
services是由于web.xml配置所得:
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
webserviceHello是由于Spring配置文件中的address属性所得:
<jaxws:endpoint id="service"
implementor="com.server.service.HelloImpl" address="/webserviceHello" />
现在服务器发布成功了,接着就写客户端程序了!
新建一个web项目,命名为;CXFClient
建一个包;com.server.dao(这个接口的包名要与服务器接口包名一样)
建一个接口:Hello(最好与服务器的接口名字一样)
代码如下:
package com.server.dao;
import javax.jws.WebService;
@WebService
public interface Hello {
//这里的方法名必须与服务器接口的方法一样
public String hello(String username);
}
新建一个测试类:Test
代码如下:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext*.xml");
Hello service = (Hello) context.getBean("webServiceClient");
System.out.println(service.hello("和谐dota"));
}
}
代码基本上完成了,现在为项目增加Spring功能,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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>apache cxf 配置 webservice 服务</description>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<listener>
<description>spring 的监听</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<description>spring 的配置文件加载路径</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
</web-app>
对应的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="webServiceClient"
address="http://localhost:8080/CXFServer/services/webserviceHello"
serviceClass="com.server.dao.Hello" />
</beans>
最后执行测试类:Test的代码,控制台会打印出:sayHello和谐dota
到此,CXF与Spring整合已经完成了!!希望能给你带来一点帮助!!
注解:Spring配置文件放在src目录下就可以了!
pasting