當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring 整合 CXF
生活随笔
收集整理的這篇文章主要介紹了
Spring 整合 CXF
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:發布服務
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/beanshttp://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/beanshttp://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>轉載于:https://www.cnblogs.com/yangjin-55/archive/2012/04/27/2786554.html
總結
以上是生活随笔為你收集整理的Spring 整合 CXF的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servlet细节
- 下一篇: [转载]如何在只能力加载的有限元程序里面