`

CXFServlet

 
阅读更多
package org.apache.cxf.transport.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
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
{
  protected void loadBus(ServletConfig sc)
  {	//获取servlet信息
    ApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc.getServletContext());
	//默认找web.xml配置文件中的"config-location"
	
	//<!-- 配置CXF的核心Servlet -->
	//<servlet>
	//	<servlet-name>cxf</servlet-name>
	//	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	//	<!-- 通过初始化参数指定cxf配置文件的位置 -->
	//	<!-- 以下不注释则调用服务时会重新加载 -->
	//	<!-- 
	//	<init-param>
	//		<param-name>config-location</param-name>
	//		<param-value>classpath:cxf-servlet.xml</param-value>
	//	</init-param>
	//	 -->
	//</servlet>
	
    String configLocation = sc.getInitParameter("config-location");
    if (configLocation == null)
      try {
		//当找不到默认的"config-location"时,去找cxf-servlet.xml文件
        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)
      {
      }
    if (configLocation != null) {
      wac = createSpringContext(wac, sc, configLocation);
    }
    if (wac != null)
      setBus((Bus)wac.getBean("cxf", Bus.class));
    else
      setBus(BusFactory.newInstance().createBus());
  }

  private ApplicationContext createSpringContext(ApplicationContext ctx, ServletConfig sc, String location)
  {
    XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
    ctx2.setServletConfig(sc);

    Resource r = ctx2.getResource(location);
    try {
      InputStream in = r.getInputStream();
      in.close();
    }
    catch (IOException e) {
      r = ctx2.getResource("classpath:" + location);
      try {
        r.getInputStream().close();
      }
      catch (IOException e2) {
        r = null;
      }
    }
    try {
      if (r != null)
        location = r.getURL().toExternalForm();
    }
    catch (IOException e)
    {
    }
    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 });
    }

    ctx2.refresh();
    return ctx2;
  }
}

 

分享到:
评论

相关推荐

    CXF servlet 发布webservice

    &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;CXFServlet &lt;url-pattern&gt;/services/* ``` 这样,CXF就会监听`/services/*`路径下的所有请求。最后,通过...

    基于CXF+servlet整合spring接口系统

    【标题】"基于CXF+servlet整合spring接口系统"揭示了这个项目的核心是构建一个集成了Spring、CXF和Servlet的Web服务接口系统。这里主要涉及的技术栈包括Spring框架、CXF作为服务消费和发布的工具,以及Servlet 3.0...

    WebServiceDemo-CXFNoSpring

    在此过程中,我们将深入探讨CXF、CXFServlet以及它们在非Spring环境中的工作原理。 **一、WebService简介** Web服务是一种通过网络进行通信的软件系统,它允许不同应用程序之间的数据交换。WebService基于开放标准...

    SpringBoot集成webService

    ServletRegistrationBean&lt;CXFServlet&gt; servlet = new ServletRegistrationBean(new CXFServlet(), "/cxf/*"); servlet.setName("CXFServlet"); return servlet; } @Bean public Bus cxfBus() { return ...

    cxf tomcat发布

    6. **配置CXFServlet**:在Tomcat的`web.xml`中配置CXFServlet,指定服务的上下文路径和服务位置。 ```xml &lt;servlet-name&gt;CXFServlet &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet ...

    CXF WebService整合Spring示例工程代码demo

    2.web工程的web.xml中配置CXFServlet &lt;!-- 设置Spring容器加载配置文件路径 --&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:applicationContext-server.xml &lt;listener-class&gt;org....

    cxf集成Spring的restful WebService接口以Json形式表现

    &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;CXFServlet &lt;url-pattern&gt;/services/* ``` 接下来,我们需要定义一个服务接口,该接口使用JAX-RS注解(如`...

    cxf开发webservice.docx

    同时,还需要配置CXFServlet,指定其为 `&lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet&lt;/servlet-class&gt;`,并设置`&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;`来确保在服务器启动时加载。接下来,在`...

    CXF结合Spring实现WebService.doc

    在上面的代码中,我们定义了一个CXFServlet来处理WebService请求,并配置了相关的参数。 使用Apache CXF和Spring框架可以轻松地实现WebService。我们可以定义服务接口,实现服务接口,并配置applicationContext.xml...

    CXfWebService开发流程

    这里,`CXFServlet`是CXF的核心Servlet,负责处理所有Web Service请求,`/services/*`定义了服务的URL路径。 3. **添加依赖** 在Maven的`pom.xml`文件中添加CXF和其他必要的依赖,例如: ```xml &lt;groupId&gt;...

    通过CXF开发webService接口(soap协议、maven项目).pdf

    `CXFServlet`是CXF提供的Servlet,负责处理所有以`/services/*`路径开头的请求。`encodingFilter`确保所有的HTTP请求和响应都使用UTF-8编码。 接下来,我们需要创建服务接口和其实现。通常,我们会定义一个Java接口...

    cxf3.0 spirng 集成笔记

    这个Web应用的配置启动了CXFServlet,使得所有以`/services/`开头的URL都会被CXF处理。`load-on-startup`元素确保CXFServlet在应用启动时加载。 **Spring配置**: 为了在Spring中注册和管理CXF服务,你需要在Spring...

    maven - spring4.1.6和cxf3.0.8 WebService整合代码

    &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;CXFServlet &lt;url-pattern&gt;/services/* &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;/WEB-INF/...

    CXF发布WebService,jboss和tomcat都能发布

    &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;CXFServlet &lt;url-pattern&gt;/services/* ``` 5. **部署到应用服务器** 将你的Web服务打包成WAR文件,...

    CXF整合spring同时支持JSON和XML配置的HelloWorld

    &lt;bean id="cxf" class="org.apache.cxf.transport.servlet.CXFServlet"&gt; &lt;load-on-startup&gt;1 ``` 在上面的配置中,`&lt;cxf:features&gt;`元素启用了JSON支持,`&lt;cxf:jsr311/&gt;`则支持JAX-RS标准,使得CXF可以...

    cxf依赖jar包.zip

    &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;CXFServlet &lt;url-pattern&gt;/services/* ``` 最后,测试Web Service。可以通过CXF的WS-*或RESTful客户端API来...

    CXF+Spring发布webservice服务的例子

    &lt;bean id="cxfServlet" class="org.apache.cxf.transport.servlet.CXFServlet"&gt; &lt;servlet-name&gt;cxf &lt;url-pattern&gt;/services/* ``` 这里,我们创建了一个CXFServlet bean,并将其映射到URL模式...

    在WebSphere发布CXF注意事项

    &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;CXFServlet &lt;url-pattern&gt;/services/* ``` 3. **JAX-WS和JAX-RS支持**:CXF支持JAX-WS(Java API for ...

    CXF创建webService接口

    &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;CXFServlet &lt;url-pattern&gt;/services/* ``` - 最后,部署到服务器并运行。 2. **客户端调用** - ...

    Apache CXF部署WebServices实例代码

    &lt;bean id="cxfServlet" class="org.apache.cxf.transport.servlet.CXFServlet"&gt; &lt;load-on-startup&gt;1 ``` 5. **部署服务**:将你的服务部署到一个支持CXF的服务器上,比如Tomcat。在web.xml中配置...

Global site tag (gtag.js) - Google Analytics