zoukankan      html  css  js  c++  java
  • Spring中使用CXF

    在Spring中采用CXF来使用WebService是很方便的,这是按照Apache官方网站上的文章写的。

     1.Web服务接口HelloWorld.java:

    Java代码  收藏代码
    1. package demo.spring;  
    2.   
    3. import javax.jws.WebService;  
    4.   
    5. @WebService  
    6. public interface HelloWorld {  
    7.     String sayHi(String text);  
    8. }  

     2.实现类HelloWorldImpl.java:

    Java代码  收藏代码
    1. package demo.spring;  
    2.   
    3. import javax.jws.WebService;  
    4.   
    5. @WebService(endpointInterface = "demo.spring.HelloWorld")  
    6. public class HelloWorldImpl implements HelloWorld {  
    7.   
    8.     public String sayHi(String text) {  
    9.         return "Hello " + text;  
    10.     }  
    11. }  

     3.Spring配置文件beans.xml:

    Xml代码  收藏代码
    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="http://www.springframework.org/schema/beans  
    5.     http://www.springframework.org/schema/beans/spring-beans.xsd  
    6.     http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">  
    7.   
    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.     <jaxws:endpoint   
    13.       id="helloWorld"   
    14.       implementor="demo.spring.HelloWorldImpl"   
    15.       address="/HelloWorld" />  
    16.         
    17. </beans>  

     4.在web.xml文件中加入:

    Xml代码  收藏代码
    1. <context-param>  
    2.     <param-name>contextConfigLocation</param-name>  
    3.     <param-value>WEB-INF/beans.xml</param-value>  
    4. </context-param>  
    5.   
    6. <listener>  
    7.     <listener-class>  
    8.         org.springframework.web.context.ContextLoaderListener  
    9.     </listener-class>  
    10. </listener>  
    11.   
    12. <servlet>  
    13.     <servlet-name>CXFServlet</servlet-name>  
    14.     <servlet-class>  
    15.         org.apache.cxf.transport.servlet.CXFServlet  
    16.     </servlet-class>  
    17.     <load-on-startup>1</load-on-startup>  
    18. </servlet>  
    19.   
    20. <servlet-mapping>  
    21.     <servlet-name>CXFServlet</servlet-name>  
    22.     <url-pattern>/*</url-pattern>  
    23. </servlet-mapping>  

     5.客户端调用时Client.java:

    Java代码  收藏代码
    1. package demo.spring.client;  
    2.   
    3. import demo.spring.HelloWorld;  
    4.   
    5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    6.   
    7.   
    8. public final class Client {  
    9.   
    10.     private Client() {  
    11.     }  
    12.   
    13.     public static void main(String args[]) throws Exception {  
    14.         ClassPathXmlApplicationContext context   
    15.             = new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});  
    16.   
    17.         HelloWorld client = (HelloWorld)context.getBean("helloWorld");  
    18.   
    19.         String response = client.sayHi("Joe");  
    20.         System.out.println("Response: " + response);  
    21.     }  
    22. }  

      client-beans.xml

    Xml代码  收藏代码
    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="http://www.springframework.org/schema/beans  
    5.     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="helloWorld" class="demo.spring.HelloWorld"   
    9.       factory-bean="clientFactory" factory-method="create"/>  
    10.       
    11.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
    12.       <property name="serviceClass" value="demo.spring.HelloWorld"/>  
    13.       <property name="address" value="http://localhost:8080/cxf2/HelloWorld"/>  
    14.     </bean>  
    15.         
    16. </beans>  

     


     源代码详见本文附件。

    • cxf2.rar (2.5 KB)
    • 描述: 此文的源代码
    • 下载次数: 1193
  • 相关阅读:
    网络七层
    微信小程序开发工具 常用快捷键
    BZOJ 1026 windy数 (数位DP)
    BZOJ 1026 windy数 (数位DP)
    CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
    CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
    HDU 3709 Balanced Number (数位DP)
    HDU 3709 Balanced Number (数位DP)
    UVA 11361 Investigating Div-Sum Property (数位DP)
    UVA 11361 Investigating Div-Sum Property (数位DP)
  • 原文地址:https://www.cnblogs.com/chenying99/p/2781043.html
Copyright © 2011-2022 走看看