CXF版本 2.7.18 +Spring3.xhtml
官方文檔:http://cxf.apache.org/docs/dynamic-clients.htmljava
http://cxf.apache.org/docs/developing-a-service.htmlweb
cxf基本jar包http://download.csdn.net/detail/q5545q/9669247 spring基本包不用說了。spring
1.隨便建立一個web項目 注意各個xml配置文件 web.xmlsql
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name>API</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:beans.xml, classpath:beanCXF.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-MVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>CXFServlet</servlet-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> <session-config> <session-timeout>60</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
*cxf的配置 /webservice/是訪問webservice接口時要添加的路徑與beanCXF.xml中address一塊兒apache
<servlet> <servlet-name>CXFServlet</servlet-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>
2.beanCXF.xml cxf配置。注意:xml文件頭xsi配置api
<?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"> <!-- cxf3之後,只須要引入cxf.xml這個配置文件便可,其餘兩個廢棄掉了 --> <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="hello" class="wan.service.helloImpl" /> <!-- webservice 客戶端代碼調用方法地址 具體看客戶端代碼 利用myeclipse自動生成 --> <jaxws:endpoint id="api" implementor="#hello" address="/api" /> <!-- <jaxws:client id="testServiceClient" serviceClass="wan.service.helloImpl" --> <!-- address="http://localhost:8080/API/webservice/api"> --> <!-- </jaxws:client> --> </beans>
3.:beans.xml web項目基本配置,同時集成mybatise 很少說spring-mvc
<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:sws="http://www.springframework.org/schema/web-services" 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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 基於註解的根包掃描路徑 --> <context:component-scan base-package="wan" /> <!-- 加載jdbc配置 --> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="url" value="${url}" /> <property name="driverClassName" value="${driver}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="maxActive" value="30" /> <property name="maxIdle" value="5" /> <property name="maxWait" value="60000" /> </bean> <!-- 聲明mybatise --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="wan.entity" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="wan.dao" /> </bean> <!-- 事務控制器 --> <!-- <bean id="txManager" --> <!-- class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> --> <!-- <property name="dataSource"> --> <!-- <ref local="dataSource" /> --> <!-- </property> --> <!-- </bean> --> <!-- 實現基於註解的事務管理 --> <!-- <tx:annotation-driven transaction-manager="txManager" /> --> </beans>
4.spring-MVC.xml 很少說session
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 註解掃描包 掃描com.zhcv包以及子包下註解爲 Controller Service Repository Compotent的類--> <context:component-scan base-package="wan"/> <!-- 開啓註解 --> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <!-- 定義視圖解析器, "/" 默認放在WebRoot下--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/page/"/> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="maxUploadSize" value="10485760"/> </bean> </beans>
看配置 就基本能明白,下面是測試代碼 1.controllermybatis
package wan.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import wan.service.helloImpl; @Controller public class helloController { @Resource(name = "helleword") helloImpl helloImpl; @RequestMapping(value = "delUser") public void delUser() { System.out.println("kkk"); helloImpl.sayHello(); } }
2.service 注意一下service註解,這樣同一個service裏的方法 webservice客戶端代碼和controller都能調用
package wan.service; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import wan.dao.UserTokenMapper; @WebService(endpointInterface = "wan.service.helleword", serviceName = "api") @Service("helleword") public class helloImpl implements helleword { @Autowired UserTokenMapper UserTokenMapper; public void sayHello() { UserTokenMapper.deleteByPrimaryKey(49); System.out.println("nihao"); } public void getHello() { System.out.println("get it"); } }
3.其餘不用說 mybatise生成dao和entity
4.經過beanCXF.xml和web.xml關於cxf的配置能夠的到訪問路徑 http://localhost:8080/API/webservice/api
11.16 cxf作webservice接口 參數封裝成一個對象