现在的位置: 首页 > 综合 > 正文

Spring 整合 CXF

2013年10月14日 ⁄ 综合 ⁄ 共 4140字 ⁄ 字号 评论关闭

一:发布服务

web.xml

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/applicationContext.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>/webService/*</url-pattern>
	</servlet-mapping>
import javax.jws.WebService;

/**
 * webservice定义 
 * 如果不指定targetNamespace 默认为包名称
 * 例如包名为 com.xx.webservice targetNamespace为 http://webservice.xx.com/
 */
@WebService(targetNamespace= "http://sayhello.webservice/")
public interface ISayHelloWebService {

	public String sayHello(String name);
	
}
webservice实现
import javax.jws.WebService;

/**
 * 如果不指定targetNamespace 默认为包名称
 * 例如包名为 com.xx.webservice targetNamespace为 http://webservice.xx.com/
 */
@WebService(targetNamespace= "http://sayhello.webservice/")
public class SayHelloWebServiceImpl implements ISayHelloWebService {

	public String sayHello(String name) {
		return "hello," + name;
	}
}
Spring 配置为文件
<?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:jaxws="http://cxf.apache.org/jaxws"
		xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


http://cxf.apache.org/jaxws

			http://cxf.apache.org/schemas/jaxws.xsd">

		<import resource="classpath:META-INF/cxf/cxf.xml"/>    
		<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> 
	       <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
  		
  		<bean id="sayHelloWebService" class="cn.xx.SayHelloWebServiceImpl" />
  		
  		<jaxws:endpoint id="sayHelloWebServiceWS" address="/sayHello">
  			<jaxws:implementor ref="sayHelloWebService" />
  		</jaxws:endpoint>
</beans>

发布服务完成 启动web容器 浏览器查看wsdl文件定义http://localhost:9090/ylnetWebservice/webService/sayHello?wsdl

二:客户端 调用webservice

import javax.jws.WebService;

/**
 * 客户端
 */
@WebService(targetNamespace= "http://sayhello.webservice/")
public interface ISayHelloWebServiceClient {

	public String sayHello(String name);
	
}
public class SayHelloClientWS extends BaseWebService {

	protected ISayHelloWebServiceClient getWebService() {
		return (ISayHelloWebServiceClient) getWebService(ISayHelloWebServiceClient.class);
	}
	
	public String sayHello(String name) {
		return getWebService().sayHello(name);
	}
}
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public abstract class BaseWebService {

	private String host;
	
	private String address;
	
	private JaxWsProxyFactoryBean getJaxWsFactory(Class<?> clazz){
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		System.out.println(getWebServiceAddress());
		factory.setAddress(getWebServiceAddress());
		factory.setServiceClass(clazz);
		return factory;
	}
	
	protected abstract Object getWebService();
	
	/**
	 * TODO 可以考虑在此缓存
	 * <br>------------------------------<br>
	 * @param clazz
	 * @return
	 */
	protected Object getWebService(Class<?>  clazz)  {
		return getJaxWsFactory(clazz).create();
	}
	
	protected String getWebServiceAddress() {
		return getHost() + getAddress();
	}
	
	/**
	 * 获得host
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * 获得address
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * 设置host
	 * @param host the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * 设置address
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}
}
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-client.xml");
		SayHelloClientWS sayHelloClientWS = applicationContext.getBean("sayHelloClientWS", SayHelloClientWS.class);
		System.out.println(sayHelloClientWS.sayHello("张三"));
	}
applicationContext-client.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"
		xsi:schemaLocation="

http://www.springframework.org/schema/beans

			http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
		
	<bean id="sayHelloClientWS" 	class="cn.soaringstar.ylnet.webservice.SayHelloClientWS" >
		<property name="host" 		value="http://localhost:9090/ylnetWebservice/webService/" />
		<property name="address" 	value="sayHello" />
	</bean>

</beans>

抱歉!评论已关闭.