真正用的时候都是需要部署在WEB服务器里面。

不能写主函数来发布了,需要借助于我们WEB.

4.配置web.xml,

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>

这个Servlet是初始化WebService用的.

创建cxf的核心配置文件cxf-servlet.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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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="hello" address="/hello" implementor="com.rl.cxf.web.server.HelloService">
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:outInterceptors>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint> </beans>

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<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"
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"> <!-- For Testing using the Spring commons processor, uncomment one of:-->
<!--
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<context:annotation-config/>
-->
<bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus" destroy-method="shutdown"/> <bean id="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor"
class="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor"/>
<bean id="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"
class="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"/>
<bean id="org.apache.cxf.bus.spring.BusExtensionPostProcessor"
class="org.apache.cxf.bus.spring.BusExtensionPostProcessor"/> </beans>

myeclipse中配置spring xml自动提示

什么时候加载cxf-servlet.xml?启动的时候加载.

关联源码

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.transport.servlet; import java.io.IOException;
import java.io.InputStream; import javax.servlet.ServletConfig; import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext; public class CXFServlet extends CXFNonSpringServlet {
private boolean busCreated;
private XmlWebApplicationContext createdContext; public CXFServlet() {
} @Override
protected void loadBus(ServletConfig sc) {
ApplicationContext wac = WebApplicationContextUtils.
getWebApplicationContext(sc.getServletContext());
String configLocation = sc.getInitParameter("config-location");
if (configLocation == null) {
try {
InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > 0) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}
if (configLocation != null) {
wac = createSpringContext(wac, sc, configLocation);
}
if (wac != null) {
setBus(wac.getBean("cxf", Bus.class));
} else {
busCreated = true;
setBus(BusFactory.newInstance().createBus());
}
} /**
* Try to create a spring application context from the config location.
* Will first try to resolve the location using the servlet context.
* If that does not work then the location is given as is to spring
*
* @param ctx
* @param sc
* @param configLocation
* @return
*/
private ApplicationContext createSpringContext(ApplicationContext ctx,
ServletConfig sc,
String location) {
XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
createdContext = ctx2;
ctx2.setServletConfig(sc); Resource r = ctx2.getResource(location);
try {
InputStream in = r.getInputStream();
in.close();
} catch (IOException e) {
//ignore
r = ctx2.getResource("classpath:" + location);
try {
r.getInputStream().close();
} catch (IOException e2) {
//ignore
r = null;
}
}
try {
if (r != null) {
location = r.getURL().toExternalForm();
}
} catch (IOException e) {
//ignore
}
if (ctx != null) {
ctx2.setParent(ctx);
String names[] = ctx.getBeanNamesForType(Bus.class);
if (names == null || names.length == 0) {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
} else {
ctx2.setConfigLocations(new String[] {location});
}
} else {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
createdContext = ctx2;
}
ctx2.refresh();
return ctx2;
}
public void destroyBus() {
if (busCreated) {
//if we created the Bus, we need to destroy it. Otherwise, spring will handleit.
getBus().shutdown(true);
}
if (createdContext != null) {
createdContext.close();
}
} }

loadBus(ServletConfig sc)方法什么时候被调用?Servlet什么时候被初始化?初始化应该调用一个方法(init()方法).

当你把这个里面的东西继承过来之后就不分什么外层内层的了,就是说都在一个类里面这些东西啊。

Spring的接口ApplicationContext是容器的接口.WebApplicationContextUtils是工具类,当然还有一个东西叫WebApplicationContext,它是ApplicationContext的一个子类,是Web容器对于Ioc容器的支持.

web.xml可以有一些参数的一些东西,可以有一些参数.

web.xml如果是手动指定路径那么configLocation不等于null.

        if (configLocation != null) {
wac = createSpringContext(wac, sc, configLocation);
}

如果configLocation不等于null,那么它就去加载初始化init File里面的路径了.

        if (configLocation == null) {
try {
InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > 0) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}

如果configLocation等于null,就去读配置文件WEB-INF/cxf-servlet.xml.配置文件默认的位置是WEB-INF/cxf-servlet.xml.

咱们初始化的时候,在Servlet里面,那就说明在web.xml,当你交代了在这个base创建的时候,init的时候,这个配置文件cxf-servlet.xml就被读取了.读取了之后还要把服务给你发布出去.

     <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService">
<!-- 输入拦截器,打印输入的消息 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>

做好了之后可以把这个服务部署在WEB服务里面.

WSDL文档是从下往上看

它会先去读WSDL文档,然后再去发送请求

这是发送的消息体:

<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHello xmlns:ns2="http://server.web.rl.com/"><arg0>李四</arg0></ns2:sayHello></S:Body></S:Envelope>

这是返回的消息体:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://server.web.rl.com/"><return>李四 hello</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>

在web.xml里面配置CXFServlet.CXFServlet是用作初始化用的,cxf-servlet.xml是核心的配置文件.核心的配置文件是用于为我们的这个服务类来做的这个配置,指定地址啊,指定这个服务类啊。


package com.rl.web.server;

import javax.jws.WebService;

@WebService //所有的webservice服务类都要加@webservice,接口的形式加在接口上

public class HelloService {
public String sayHello(String name){
return name + " hello";
}
}
<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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" />
<!--
address:tomcat的host http://ip:port/projectName/service/后面的一端路径
implementor:指定具体的服务的类
-->
<jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService">
<!-- 输入拦截器,打印输入的消息 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern> <!-- 拦截这种请求 -->
</servlet-mapping>
</web-app>
package com.rl.cxf.web.client;

import com.rl.web.server.HelloService;
import com.rl.web.server.HelloServiceService; public class WebClient {
public static void main(String[] args) {
HelloServiceService hss = new HelloServiceService();
HelloService hs = hss.getHelloServicePort();
String result = hs.sayHello("李四");
System.out.println(result);
}
}

day63-webservice 06.在web项目中发布以类的形式发布webservice的更多相关文章

  1. 获取Web项目中的控制器类以及类中Action方法

    前言 在使用时需要修改命名空间.需要过滤控制器.需要过滤Action方法.结果生成表的插入语句. 代码 public ActionResult ReloadData() { #region 获取所有的 ...

  2. 在web项目中发布jaxws

    概述 在web项目中发布基于jaxws的webservice. 参考文章:用JAX-WS在Tomcat中发布WebService 参考文章说,如果不是servlet3.0及以上,需要配置servlet ...

  3. 在web项目中使用cxf开发webservice,包含spring支持

    本文主要介绍了,如何使用cxf内置的例子,学会开发webserivce,在web项目中使用,且包含spring支持. webserivce的开发可以使用cxf或者axis,好像还有httpclient ...

  4. 转 web项目中的web.xml元素解析

    转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45)   阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...

  5. JAVA WEB项目中各种路径的获取

    JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报  分类: JAVA开发(41)  1.可以在s ...

  6. java web项目中 获取resource路径下的文件路径

    public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...

  7. Log4j2在WEB项目中配置

    最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...

  8. (转)关于java和web项目中的相对路径问题

    原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...

  9. Axis2在Web项目中整合Spring

    一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...

随机推荐

  1. CSS的常用属性(二)

    盒子模型之边框 border-(top/bottom/left/right)-style: solid 边框的风格 如(solid 实线,dotted 点线,dashed 虚线) border-top ...

  2. less 安装和webstorm的使用

    1.less 的安装 npm install -g less 2.less安装成功 3.less安装成功后,在webstorm中进行配置.file——>settings:弹出settings框, ...

  3. mysql数据库之存储过程入门

    引用:百度百科 存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存 ...

  4. Deutsch lernen (15)

    1.    unterscheiden - unterschied - unterschieden  区别,区分:(能够)分清 Die beiden Begriffe sind nur schwer ...

  5. (转)OpenLayers3基础教程——OL3基本概念

    http://blog.csdn.net/gisshixisheng/article/details/46756275 OpenLayers3基础教程——OL3基本概念 从本节开始,我会陆陆续续的更新 ...

  6. HDU_5833_高斯消元

    参考自:http://www.cnblogs.com/flipped/p/5771492.html 自己做的时候不知道如何求种数.看了题解,感觉思路灰常巧妙.同时也感觉这是一道好题. 精髓在于转化为线 ...

  7. Content-Encoding与Content-Type的区别

    RFC 2616 for HTTP 1.1 specifies how web servers must indicate encoding transformations using the Con ...

  8. Tomcat 服务器中jsp页面乱码

    <Connector port="80" protocol="HTTP/1.1"               connectionTimeout=&quo ...

  9. pyqt5 做的小程序,可以用来UI做个小demo

    #!/usr/bin/python3# -*- coding: utf-8 -*- """ZetCode PyQt5 tutorial This program crea ...

  10. 实现Modbus ASCII多主站应用

    1.更新设计关于原来的协议栈在Modbus ASCII主站应用时所存在的局限性与Modbus RTU也是一样的,所以我们不分析它的不足,只讨论更新设计.我们将主站及其所访问的从站定义为通用的对象,而当 ...