很早很早之前,就初步学习了WebService,感觉还是比较“好”的。
  使用Web服务,感觉就像普通API一样,和HTTP接口比较起来。
  WebService有个很大的局限,就是事务,分布式事务麻烦程度就上升了不少,暂时没有搞过。
  
  最近1年做的2个比较完整的项目,都有WebService的配置,只不过,都是别人配置好的。
  
  别人的,终究是别人的。
  
  作为一名热爱学习并学以致用的程序员,我也来亲自搞个配置~
  
  下面的例子,是我今天亲自一步步配的,而且运行成功了。
  
  CSDN下载地址:http://download.csdn.net/detail/fansunion/9218657
  (我打算今后在这个基础上,进一步完善示例)
  
 一、WebServiceClient接口项目
    其它项目如果想使用Web服务,直接调用接口就行了,根本不需要关注服务的实现。

	@WebService
public interface UserFacade {
String query();
}

接口,已经做到最简了。查询一个字符串,就表明Web服务调用成功了。
   
 

 package cn.fansunion.webservice.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.fansunion.webservice.UserFacade; public class WebServiceTest { public static void main(String[] args) {
//初始化Spring上下文,webservice文件位于src目录下(也可以说是Classpath下)
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:/spring-webservice.xml");
//根据id获得bean,我感觉是这个“jaxws:client id="remoteUserFacade"”WebService语法和Spring语法的结合
UserFacade userFacade=(UserFacade) ac.getBean("remoteUserFacade");
//查询,返回并打印字符串“WebService”
System.out.println(userFacade.query());
} }

spring-webservice.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 启用autowire -->
<context:annotation-config />
<context:component-scan base-package="com.fansunion.webservice" />
<jaxws:client id="remoteUserFacade"
address="http://localhost:8080/webService/UserFacade" serviceClass="cn.fansunion.webservice.UserFacade" /> </beans>

比较关键的是jaxws:client id="remoteUserFacade",这个是WebService的核心配置。
  另外,需要注意http://cxf.apache.org/jaxws的xsi和xmlns的配置,如果没有,应该会报错的。
  
  pom.xml配置
 

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>

二、WebServiceImpl接口的实现项目

package cn.fansunion.webservice.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Service;

import cn.fansunion.webservice.UserFacade;

@WebService(endpointInterface = "cn.fansunion.webservice.UserFacade", serviceName = "UserFacade")
@Service("userFacadeImpl")
public class UserFacdeImpl implements UserFacade { @Override
public String query() {
return "WebService";
} }

和普通的Java接口实现类相比,就多了WebService注解。
看样子,endpointInterface和serviceName很关键。
目前还不太清楚这2个属性是否是必须的,根据我已有的经验猜测,都是可选的,如果不写,会按照一定的规则用默认的名字和类。
刚刚写完,我就觉得不对了。
endpointInterface按说是必选的,当然如果不写,程序是完全可以分析得出来的,因为有“implements”。
有兴趣的,自己百度。

UserFacdeImpl需要作为Web的方式发布,接下来配置web.xml文件。
Spring配置文件,基本都懂,主要就是配置了WebService的CXFServlet。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-webservice.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/webService/*</url-pattern>
</servlet-mapping> </web-app>

spring-webservice.xml

<!-- Import 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" /> <bean id="userFacade"
class="cn.fansunion.webservice.impl.UserFacdeImpl">
</bean>
<jaxws:server id="webserviceUserFacade" serviceBean="#userFacade" address="/UserFacade">
</jaxws:server>
<!-- 启用autowire -->
<context:annotation-config />
<context:component-scan base-package="com.fansunion.webservice" />

引入CXF的xml配置,配置bean,最重要的还是“jaxws:server”,和Client项目的“jaxws:client”相对应吧~

pom.xml
和之前的完全一致~

三、简要回顾下
UserFacadeClient项目:就一个接口UserFacade
UserFacadeImpl项目:接口实现UserFacadeImpl、spring-webservice.xml、pom.xml、web.xml
测试项目:WebServiceTest、pom.xml、spring-webservice.xml
为了方便,我们把测试项目,直接和Client接口项目放在了一起~

四、测试和运行流程
1.启动UserFacadeImpl这个Web项目
  2015-10-27 22:39:02.195:INFO:oejs.Server:jetty-8.1.14.v20131031
2015-10-27 22:39:02.562:INFO:/:No Spring WebApplicationInitializer types detected on classpath
2015-10-27 22:39:03.087:INFO:/:Initializing Spring root WebApplicationContext
十月 27, 2015 10:39:03 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
十月 27, 2015 10:39:03 下午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Oct 27 22:39:03 CST 2015]; root of context hierarchy
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-webservice.xml]
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf/cxf-extension-soap.xml]
十月 27, 2015 10:39:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/cxf/cxf-servlet.xml]
十月 27, 2015 10:39:04 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://impl.webservice.fansunion.cn/}UserFacade from class cn.fansunion.webservice.UserFacade
十月 27, 2015 10:39:04 下午 org.apache.cxf.bus.spring.OldSpringSupport logWarning
WARNING: Import of META-INF/cxf/cxf-extension-soap.xml has been deprecated and is unnecessary.
十月 27, 2015 10:39:04 下午 org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be /UserFacade
十月 27, 2015 10:39:04 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 1644 ms
2015-10-27 22:39:04.839:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
------------------------------------------------
Jetty startup finished in 3.1 s.
Used memory: 5.5 MB of 121.8 MB (1.8 GB maximum)
Console available: type "help".
------------------------------------------------

2.运行WebServiceTest这个Java应用程序
十月 27, 2015 10:39:08 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2484e723: startup date [Tue Oct 27 22:39:08 CST 2015]; root of context hierarchy
十月 27, 2015 10:39:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-webservice.xml]
十月 27, 2015 10:39:09 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://webservice.fansunion.cn/}UserFacadeService from class cn.fansunion.webservice.UserFacade
WebService

3.也可以访问http://localhost:8080/webService/UserFacade/query?wsdl
查看这个WebService的定义

我想通过浏览器访问http://localhost:8080/webService/UserFacade/query?wsdl类似的URL,直接显示返回结果。
好像不太行哦,后面再研究吧~

五、最后说明
1.为了简单起见,这2个项目都非常地简单,完全没有与WebService无关紧要的代码和配置。
麻雀虽小,五脏俱全哦
2.为了简单起见,服务器用的是localhost,而且是写死的,你懂的~
3.看服务和应用程序的启动日志,是非常有价值的~

这次就先总结到这吧~

六、源码下载地址
http://download.csdn.net/detail/fansunion/9218657

小雷FansUnion-程序员一枚
2015年10月27日
湖北-武汉-循礼门
QQ:240370818
微信:FansUnion

Java-Spring-WebService最基础的配置示例的更多相关文章

  1. 在上已个Java Spring MVC项目基础上加MyBatis

    代码目录: /Users/baidu/Documents/Data/Work/Code/Self/HelloSpringMVC 1. 首先在resource目录加上jdbc.properties: d ...

  2. Spring dbcp连接池简单配置 示例

    一.配置db.properties属性文件 #database connection config connection.username=sa connection.password=sa conn ...

  3. Spring操作指南-IoC基础环境配置(基于注解自动装配)

    项目源码:http://code.taobao.org/p/LearningJavaEE/src/LearningSpring001%20-%20Automatically%20wiring%20be ...

  4. Spring操作指南-IoC基础环境配置(基于注解手动装配)

    Source: http://code.taobao.org/p/LearningJavaEE/src/LearningSpring002%20-%20Wiring%20beans%20with%20 ...

  5. Spring操作指南-IoC基础环境配置(基于XML)

  6. 将 Java Spring Framework 应用程序迁移到 Windows Azure

    我们刚刚发布了一个新教程和示例代码,以阐述如何在Windows Azure中使用 Java 相关技术.在该指南中,我们提供了分步教程,说明如何将 Java Spring Framework 应用程序( ...

  7. 学习笔记_J2EE_SSM_01_spring+springMVC+Mybatis整合_XML配置示例

    spring+springMVC+Mybatis整合_XML配置示例 1.概述 spring+springMVC+Mybatis整合  XML配置方式 1.1 测试环境说明 名称 版本 备注 操作系统 ...

  8. 【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑

    编写Java Spring Boot应用,通过配置logging.path路径把日志输出在指定的文件夹中. 第一步:通过VS Code创建一个空的Spring Boot项目 第二步:在applicat ...

  9. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

随机推荐

  1. [TJOI2013]单词 AC 自动机

    题目描述: 小张最近在忙毕设,所以一直在读论文. 一篇论文是由许多单词组成但小张发现一个单词会在论文中出现很多次,他想知道每个单词分别在论文中出现了多少次. 题解: AC 自动机裸题,将所有字符串读入 ...

  2. [POI2002][HAOI2007]反素数 数论 搜索 好题

    题目描述: 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4, ...

  3. 使用 validate 进行输入验证

    validate 官方教程网址: http://www.runoob.com/jquery/jquery-plugin-validate.html 在表单页面引入两个核心 js 文件 #官方的两个文件 ...

  4. HDU-1023 Train Problem II 卡特兰数(结合高精度乘除)

    题目链接:https://cn.vjudge.net/problem/HDU-1023 题意 卡特兰数的应用之一 求一个长度为n的序列通过栈后的结果序列有几种 思路 一开始不知道什么是卡特兰数,猜测是 ...

  5. 洛谷 P3199 [HNOI2009]最小圈

    P3199 [HNOI2009]最小圈 题目背景 如果你能提供题面或者题意简述,请直接在讨论区发帖,感谢你的贡献. 题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点 ...

  6. LeetCode -- 最大连续乘积子序列

    问题描写叙述: 给定数组,找出连续乘积最大值的子序列.比如 0,-1,-3.-2.则最大连续乘积为6= (-3) * (-2) 实现思路此题与最大连续和的子序列问题相似,也可通过找到递推公式然后用DP ...

  7. JSR 303 - Bean Validation 简单介绍及用法

    一.JSR-303简单介绍 JSR-303 是 JAVA EE 6 中的一项子规范.叫做 Bean Validation,官方參考实现是Hibernate Validator. 此实现与 Hibern ...

  8. void指针 (补充)

    void* 表示空指针.空指针不能直接使用,比如,指针的加减法操作(由于不知道类型,无法确定偏移的大小).某些情况下.不知道指针的类型,先用void*来取代,依据须要再强制转换成须要的指针类型使用. ...

  9. DNS A记录和CNAME记录

    参考文章:http://blog.xieyc.com/differences-between-a-record-and-cname-record/ A (Address) 记录是用来指定主机名(或域名 ...

  10. 使用powerdesigner建模时设置主键自增的问题

    研究了一下,其实只要双击表,选择columns,再双击在你所要设为自增型的键上(比如你的id)或者右键选择Properties,弹出一个ColumnProperties 对话框,我们看到有标识 ide ...