CXF 不依赖spring的实现

前端之家收集整理的这篇文章主要介绍了CXF 不依赖spring的实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cxf 也可以不依赖spring实现webservice

package com.servlet;

import javax.servlet.ServletConfig;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.frontend.Serverfactorybean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import com.Hello;
import com.HelloImpl;

public class WebServelet extends CXFNonSpringServlet { //主要是这一个继承CXFNonSpringServlet 
    
    @Override
    protected void loadBus(ServletConfig sc) {
        super.loadBus(sc);
        Bus b=getBus();
        BusFactory.setDefaultBus(b);
        HelloImpl hi=new HelloImpl();//实现类
        Serverfactorybean sfb=new Serverfactorybean(); //server工厂
        sfb.setServiceClass(Hello.class);// 接口类
        sfb.setAddress("/hi"); //服务请求路径
        sfb.setServiceBean(hi);
        sfb.create();
    }
}

web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  <servlet>
      <servlet-name>cxfServlet</servlet-name>
      <servlet-class>com.servlet.WebServelet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>cxfServlet</servlet-name>
      <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>

启动服务的时候就开启webservice接口

public static void main(String[] args) {
         JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();
            factory.setServiceClass(Hello.class);
            factory.setAddress("http://localhost:8080/Temp1/ws/hi?wsdl");
            Hello client = (Hello)factory.create();
            System.out.println(client.say("foo"));
    }

猜你在找的设计模式相关文章