CXF 不依賴spring的實現

cxf 也能夠不依賴spring實現webservicejava

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 配置web

<?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接口spring

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"));
    }
相關文章
相關標籤/搜索