WebService認識三(Spring整合)

前言:因爲通常情況下,我們都是用Spring來整合其它框架,所以這裏我們不在一、二的基礎上進行操作,而是新建一個工程,然後拷貝src下的java代碼和db.properties文件到新的工程中,再添加jar。

1:導入spring的jar,這裏閒麻煩,把3.1的dist裏面的jar都導進來了,在導入cxf的jar如下:

spring的jar這裏就不展示了

2:建立beans.xml文件,保存在src下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    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-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.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="userSerivce" class="org.wjlmgqs.service.imp.UserServiceImp"/>
	<jaxws:server id="userWebService" serviceClass="org.wjlmgqs.service.UserService" address="/user">
	    <jaxws:serviceBean>
	        <ref bean="userSerivce"/>
	    </jaxws:serviceBean>
		<!--
		<jaxws:inInterceptors>
        	<ref bean="inMessageInterceptor"/>
	    </jaxws:inInterceptors>
	    <jaxws:outInterceptors>
	        <ref bean="outLoggingInterceptor"/>
	    </jaxws:outInterceptors>
		-->
	</jaxws:server>	
<!-- 上面通過jaxws:server來發布服務,還可以通過下面的方式實現	
	<bean id="userSerivce" class="org.wjlmgqs.service.imp.UserServiceImp"/>
	<jaxws:endpoint id="userWebService" implementor="#userSerivce" address="/user"/>
-->		
</beans>

3:修改web.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<display-name>CXF Servlet</display-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webservice/*</url-pattern>
	</servlet-mapping>
</web-app>

4:瀏覽器訪問:http://localhost:8080/SpringWebService/webservice/user?wsdl

結果同WebService認識二一樣

該工程源碼下載:下載源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章