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>
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://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>
package test;import javax.jws.WebService;@WebServicepublic interface IHelloWorld {public String sayHello(String name);}
package test;import javax.jws.WebService;@WebServicepublic class HelloWorldImpl implements IHelloWorld {public String sayHello(String name) {System.out.println("sayHello is called by " + name);return "Hello " + name;}}
<jaxws:endpoint id="helloWorld" implementor="test.HelloWorldImpl" address="/HelloWorld" />
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://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" servicename="code">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"));}}