文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>CXF与spring集成(转载)

CXF与spring集成(转载)

时间:2010-06-19  来源:liugao_0614

CXF与spring集成

CXF与spring集成
http://blog.csdn.net/pengchua/archive/2008/07/30/2740572.aspx
1. 新建web project ,并加入apache-cxf-2.0.7\lib所有包,编写要发布的web service 接口和实现.这一步,与前面一样。
Java代码
  1. import javax.jws.WebService;   
  2.   
  3. @WebService    
  4.   
  5. public interface HelloWorld {     
  6.   
  7.      public String sayHello(String text);     
  8.   
  9. }   
  10.   
  11. import javax.jws.WebService;     
  12.   
  13. @WebService(endpointInterface="test.HelloWorld")     
  14.   
  15. public class HelloWorldImpl implements HelloWorld {     
  16.   
  17.       public String sayHello(String text) {     
  18.   
  19.                   return "Hello" + text ;     
  20.   
  21.     }     
  22.   
  23.   }   
import javax.jws.WebService;

@WebService 

public interface HelloWorld {  

     public String sayHello(String text);  

}

import javax.jws.WebService;  

@WebService(endpointInterface="test.HelloWorld")  

public class HelloWorldImpl implements HelloWorld {  

      public String sayHello(String text) {  

                  return "Hello" + text ;  

    }  

  } 


@WebService 注解表示是要发布的web 服务

2. 在spring-cxf.xml配置发布的web service

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   
  8.     <import resource="classpath:META-INF/cxf/cxf.xml" />    
  9.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    
  10.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    
  11.     
  12.     <bean id="hello" class="test.HelloWorldImpl" />    
  13.     <jaxws:endpoint id="helloWorld" implementor="#hello"    
  14.         address="/HelloWorld" />    
  15.   </beans>  
<?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" /> 
 
    <bean id="hello" class="test.HelloWorldImpl" /> 
    <jaxws:endpoint id="helloWorld" implementor="#hello" 
        address="/HelloWorld" /> 
  </beans>

注意:<jaxws:endpoint id="helloWorld" implementor="#hello"
        address="/HelloWorld" />
id:指在spring配置的bean的ID.

Implementor:指明具体的实现类.

Address:指明这个web service的相对地址,

3.  配置web.xml文件:

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee      
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    
  6.     <context-param>    
  7.         <param-name>contextConfigLocation</param-name>     
  8.         <param-value>classpath:spring-cxf.xml</param-value>    
  9.     </context-param>    
  10.     
  11.     <listener>    
  12.         <listener-class>    
  13.          org.springframework.web.context.ContextLoaderListener    
  14.         </listener-class>    
  15.     </listener>    
  16.           
  17.     <servlet>    
  18.         <servlet-name>CXFServlet</servlet-name>    
  19.         <servlet-class>    
  20.             org.apache.cxf.transport.servlet.CXFServlet     
  21.         </servlet-class>    
  22.         <load-on-startup>1</load-on-startup>    
  23.     </servlet>    
  24.     
  25.     <servlet-mapping>    
  26.         <servlet-name>CXFServlet</servlet-name>    
  27.         <url-pattern>/*</url-pattern>    
  28.     </servlet-mapping>    
  29. </web-app>   
<?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>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

4.部署到tomcat服务器,输入:http://localhost:8080/<web-app-name>/ HelloWorld?wsdl,将显示这个web service的wsdl.
注意:如果web.xml配置<servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
则访问地址为:http://localhost:8080/<web-app-name>/ws/ HelloWorld?wsdl
5. 下面就开始创建一个客户端程序,访问这个web service, 同样新建java project ,并加入apache-cxf-2.0.7\lib所有包. 创建与具体webservice技术无关的业务接口HelloWorld.java

public interface HelloWorld { 

     public String sayHello(String text); 

}

6.配置spring-client.xml
Java代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.     xsi:schemaLocation="   
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">   
  7.     
  8.     <bean id="client" class="test.HelloWorld"  
  9.       factory-bean="clientFactory" factory-method="create"/>   
  10.       
  11.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">   
  12.       <property name="serviceClass" value="test.HelloWorld"/>   
  13.       <property name="address" value="http://localhost:9000/helloWorld"/>   
  14. <!-- 这个地方的地址一定要注意,正确的-->   
  15.     </bean>   
  16.         
  17. </beans>  
<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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
 
    <bean id="client" class="test.HelloWorld"
      factory-bean="clientFactory" factory-method="create"/>
   
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
      <property name="serviceClass" value="test.HelloWorld"/>
      <property name="address" value="http://localhost:9000/helloWorld"/>
<!-- 这个地方的地址一定要注意,正确的-->
    </bean>
     
</beans>

7.测试:
Java代码
  1. import org.springframework.context.ApplicationContext;   
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  3.     
  4. import test.HelloWorld;   
  5.     
  6. public class Test {     
  7.         
  8.     public static void main(String[] args) {     
  9.     
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext(     
  11.                 "spring-client.xml");     
  12.         HelloWorld client = (HelloWorld) ctx.getBean("client");     
  13.         String result = client.sayHello("你好!");     
  14.         System.out.println(result);     
  15.     }     
  16. }   
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载