CXF版本 2.7.18 +Spring3.x
官方文档:http://cxf.apache.org/docs/dynamic-clients.html
http://cxf.apache.org/docs/developing-a-service.html
cxf基本jar包http://download.csdn.net/detail/q5545q/9669247 spring基本包不用说了。
1.随便创建一个web项目 注意各个xml配置文件 web.xml
<?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一起
<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配置
<?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 不多说
<?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 不多说
<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.controller
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接口 参数封装成一个对象