CXF2.7.3 与spring 3集成 .

1 需要的jar包

cxf包括:cxf-2.7.3.jar  wsdl4j-1.6.2.jar  xmlschema-core-2.0.3.jar  neethi-3.0.2.jar httpasyncclient-4.0-beta3.jar httpclient-4.2.1.jar httpcore-4.2.2.jar httpcore-nio-4.2.2.jar

spring包括:spring-aop-3.0.7.RELEASE.jar spring-asm-3.0.7.RELEASE.jar spring-beans-3.0.7.RELEASE.jar spring-context-3.0.7.RELEASE.jar spring-core-3.0.7.RELEASE.jar spring-expression-3.0.7.RELEASE.jar spring-jms-3.0.7.RELEASE.jar spring-tx-3.0.7.RELEASE.jar spring-web-3.0.7.RELEASE.jar

其它:commons-logging-1.1.1.jar

 

2 在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"> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:spring-cxf.xml</param-value> 
    </context-param> 
 
    <listener> 
        <listener-class> 
         org.springframework.web.context.ContextLoaderListener
        </listener-class> 
    </listener> 
      
    <servlet> 
        <servlet-name>CXFServlet</servlet-name> 
        <servlet-class> 
            org.apache.cxf.transport.servlet.CXFServlet  
        </servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
 
    <servlet-mapping> 
        <servlet-name>CXFServlet</servlet-name> 
        <url-pattern>/webservice/*</url-pattern>    
    </servlet-mapping> 
</web-app>

 

3 创建服务端webservice类

1)创建IHelloWorld接口

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {
 public String sayHello(String name);
}

2)创建IHelloWorld接口的实现类HelloWorldImpl

import javax.jws.WebService;


public class HelloWorldImpl implements IHelloWorld {

 @Override
 public String sayHello(String name) {
  System.out.println("say hello is called");
  return "hello "+name;
 }

}

4)创建客户端访问webservice类

import org.springframework.context.ApplicationContext;

public class Client {
 public static void main(String[] args){

  ApplicationContext context = new ClassPathXmlApplicationContext("spring-cxf.xml"); 
  IHelloWorld helloWorld=(IHelloWorld)context.getBean("helloWorldClient");
  System.out.println(helloWorld.sayHello("Test"));
 }
}

 

3 创建spring配置文件spring-cxf.xml

<?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:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 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://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://cxf.apache.org/jaxws
 
http://cxf.apache.org/schemas/jaxws.xsd"

           <!--CXF配置-->
          <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"/>

        

           <!--服务端发布的webservice-->
           <jaxws:endpoint id="helloWorld" implementor="openProjectTest.cxf.HelloWorldImpl" address="/HelloWorld"/>

 

           <!--客户端调用的webservice-->
           <jaxws:client id="helloWorldClient" address="http://localhost:8080/web工程名/webservice/HelloWorld" serviceClass="openProjectTest.cxf.IHelloWorld"/>

</beans>

 

4 测试

1)访问以下地址验证服务端是否配制成功

http://localhost:8080/web工程名/webservice/HelloWorld?wsdl  如果可看到wsdl的xml文件则成功

http://localhost:8080/web工程名/webservice  直接访问该地址可查看系统提供几个webservice服务

2)运行Client类输出 hello Test 则客户端连接成功

你可能感兴趣的:(CXF2.7.3 与spring 3集成 .)