微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 
	private static final long serialVersionUID = 1L;

	@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>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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"));
	}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐