天天看點

webService內建spring

需要的maven依賴

<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>2.7.18</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>2.7.18</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>2.7.18</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.8.RELEASE</version>
		</dependency>
           

服務端

釋出方法的實體類

package cn.et;

import org.springframework.stereotype.Service;

@Service
public class HelloImp implements Hello {

	@Override
	public String toHello(String s) {	
		return "hello "+s;
	}
}
           

實體類的接口

package cn.et;

import javax.jws.WebService;

@WebService
public interface Hello {

	public abstract String toHello(String s);

}
           

spring.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:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<context:component-scan base-package="cn"></context:component-scan>
	<!-- 表示要使用 已經定義好的spring xml檔案 初始化内部類 比如  JaxWsServerFactoryBean-->
	<!-- <import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  -->
	<!--給釋出的方法的類 注冊通路路徑與執行個體-->
	<jaxws:endpoint id="myservice" implementor="#helloImp" address="/test"></jaxws:endpoint>
</beans>
           

web.xml配置

<context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  <servlet>
  	<servlet-name>CxfServlet</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>CxfServlet</servlet-name>
  	<url-pattern>/myWeb/*</url-pattern>
  </servlet-mapping>
           

用戶端

代理接口

package cn.et;

import javax.jws.WebService;

@WebService
public interface MyClient {
	
	public abstract String toHello(String s);
}
           

spring.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:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<!--給代理指定要調用類的位址 進行執行個體化-->
	<jaxws:client id="myweb" serviceClass="cn.et.MyClient" address="http://localhost/ws/myWeb/test?wsdl"></jaxws:client>
</beans>
           

用戶端

因為隻有一個簡單的調用是以有main方法啟動了

package cn.et;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext cxac=new ClassPathXmlApplicationContext("classpath:spring.xml");
		MyClient bean = (MyClient)cxac.getBean("myweb");
		String hello = bean.toHello("world");
		System.out.println(hello);
	}

}