`
wenxiang_tune
  • 浏览: 47724 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

cxf+spring 例子代码

阅读更多

cxf2.3.3

eclipse helios

spring cxf2.3.3自带spring3.0.5

包的引用和配置神马的不解释了

首先创建一个项目,如图,选择CXF 会添加cxf支持

如下图,参照网上例子创建cxftest接口和其实现类cxftestimpl并实现其方法hello

然后创建spring文件,applicationContext.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: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://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="cxftest" implementor="com.cxf.test.CxfTestImpl" address="/cxftest" />  
</beans>
 

 

于webcontent目录下

接着创建客户端配置文件,这个主要用来测试用作对客户端的支持

如下:

 

<?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-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<bean id="client" class="com.cxf.test.CxfTest"  
        factory-bean="clientFactory" factory-method="create" />  
  
    <bean id="clientFactory"  
        class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
        <property name="serviceClass" value="com.cxf.test.CxfTest" />  
        <property name="address" value="http://localhost:8080/cxfpro/cxftest" />
        </bean>
</beans>
 

然后创建Client测试类

 

package com.cxf.test;

import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class Client {
	 public static Client self = new Client();  
     private CxfTest client = null;  
       
    private Client() {  
           // START SNIPPET: client  
           ApplicationContext context = new ClassPathXmlApplicationContext(  
                   new String[]{"com/cxf/test/client-beans.xml"});  
           client = (CxfTest) context.getBean("client");  
       }  
       
 
       public CxfTest getServer() {  
             
           return client;  
             
       }  
 
       public String getText(String text) throws Exception {  
           String response = getServer().hello(text);  
           return response;  
       }  
 
 
   /** 
    * @param args 
    */  
   public static void main(String[] args) {  
       // TODO Auto-generated method stub  
       try {  
             
           System.out.println(self.getText("你是个锤子!"));  
       } catch (Exception e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }  
         
   }  
 
}  

 

 最后肯定要配置WEB.XML

添加cxf支持

 

  <!-- 添加CXF支持 -->
   <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <display-name>CXF Servlet</display-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>  

 

 添加spring支持

 

  <!-- 添加spring支持 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<!-- spring监听器,初始化web应用上下文 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 扩展spring bean的作用域有request,session,global session等 -->
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	<!-- Spring 刷新Introspector防止内存泄露 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	
	<!-- spring字符编码过滤器 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

 除红色部分外,其他可不要,我这省事呢!

接着用ECLIPSE创建一个SERVERS

选择TOMCAT6.0

将项目添加到SERVERS中,运行项目,这时启动了服务端。如果不抛异常,运行客户端CLASS(有main入口),直接作为APPLICATION运行。

一串红色信息输出后就是你想要的结果。很快,如果eclipse成功配置了CXF环境。

接着我知道服务端和客户端是相对独立运行的,一般情况下客户端需要发布出去。于是

通过wsdl2java可获得其client resources

命令如下:进入到cxf ->bin 

wdsl2java -d e:\test -client http://localhost:8080/cxfpro/cxftest?wsdl

在e盘的test目录下就生成了如下

或许更为简单的地方在这里:http://blogold.chinaunix.net/u2/73798/showart_2002108.html,希望可以一起探讨,我是刚入门的小菜!3KS

分享到:
评论

相关推荐

    cxf+spring实现webservice例子

    cxf+spring实现webservice的例子,在eclipse开发环境上测试通过,由于上传限制,没有传lib,只把代码上传了,有需要的可以给我留言,我给单发!

    webservice cxf 整合spring例子源代码

    webservice cxf 整合spring例子源代码.相关博客的介绍: http://blog.csdn.net/dream_broken/article/details/35331949

    cxf 集成spring例子java代码

    将cxf集成到spring中。用到的工具eclipse3.3 maven2

    mybatis+spring+cxf Webservice框架

    mybatis+spring+cxf webservice服务 项目框架代码例子,绝对是可以参考的,信不信,只有下载之后才知道

    CXF和Spring结合的客户端例子

    这个代码例子是为了调用CXF提供的WS服务,所以先要下载“CXF和Spring结合的例子”,并将服务器(tomcat)运行起来,在利用本客户端测试,本客户端是根据服务端发布的WSDL文件,用wsdl2java直接生成java代码,加入到...

    Spring+CXF开发WebService

    使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻...

    spring 集成 cxf 简单例子

    源代码 博文链接:https://qsx1982-126-com.iteye.com/blog/629191

    spring整合cxf服务

    自己写的spring和cxf整合的例子,为rest风格的。里面包含全部源代码和lib包,导入myeclipse即可运行,既然要那么多的分了,肯定是好的东西。

    webService CXF集成例子

    webService CXF不依赖Spring集成,包含客户端、服务端代码。使用3.1.7版的CXF包。demo可以直接运行

    使用 CXF 做 webservice 简单例子 - 烽火编程 - 博客园

    Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的...

    使用 CXF 做 webservice 简单例子

    apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的...

    Java SSH2框架的CRUD经典例子

    struts2+ spring3.0+ hibernate3.3在myEclipse8.5下的例子,实现增删改查功能。

    wsdl2java源码-Axis1WebService:Axis1WebService例子,通过wsdl文件生成服务器端代码并部署

    CXF,Spring WS,都不能满足需求; 以 获取手机号信息的wsdl为例子 生成过程 先将tool\asix1.4\asix\WEB-INF拷贝到新建web项目的WEB-INF cd tool wsdl2java.bat 生成服务器端源码,以及deploy.wsdd,undeploy.wsdd 将...

    Java EE常用框架.xmind

    四、CXF框架可以与spring无缝连接,就不用我们自己Endpoint了。它还能记录日志之类的 五、我们还可以使用Idea下的webservice,能够使用图形画面的方式获取本地代理和生成WSDL文件。 Activiti 介绍 ...

Global site tag (gtag.js) - Google Analytics