欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CXF整合Spring发布WebService实例

程序员文章站 2022-04-04 12:33:27
...

WebService发布实例实现过程
1.所需jar包
aopalliance.jar
commons-codec-1.6.jar
commons-collections-3.2.1.jar
commons-dbcp-1.4.jar
commons-lang-2.3.jar
commons-logging-1.1.1.jar
commons-pool.jar
cxf-core-3.0.14.jar
cxf-rt-bindings-soap-3.0.14.jar
cxf-rt-databinding-jaxb-3.0.14.jar
cxf-rt-frontend-jaxws-3.0.14.jar
cxf-rt-frontend-simple-3.0.14.jar
cxf-rt-transports-http-3.0.14.jar
cxf-rt-transports-udp-3.0.14.jar
cxf-rt-ws-addr-3.0.14.jar
cxf-rt-wsdl-3.0.14.jar
cxf-rt-ws-policy-3.0.14.jar
log4j-1.2.16.jar
mybatis-3.2.0.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.1.7-bin.jar
neethi-3.0.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
stax2-api-3.1.4.jar
woodstox-core-asl-4.4.1.jar
wsdl4j-1.6.3.jar
xmlschema-core-2.2.2.jar


2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>DzWebService</display-name>

<!-- 配置监听器,用于初始化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- ServletContext初始化参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/applicationContext.xml
</param-value>
</context-param>

<!-- 字符过滤,防止添加到数据库的数据为乱码 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Servlet初始化参数,配置springmvc模块 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

    <!-- cxf配置 -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


3.applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- 自动扫描包 ,将带有注解的类 纳入spring容器管理 --> 
<context:component-scan base-package="com.soft" />

<!-- 配置数据源属性文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

<!-- dbcp 数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc_driver}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- 初始化连接 -->
<property name="initialSize" value="${jdbc_initialSize}" />
<!-- 最大空闲连接 -->
<property name="maxIdle" value="${jdbc_maxIdle}" />
<!-- 最小空闲连接 -->
<property name="minIdle" value="${jdbc_minIdle}" />
<!-- 最大连接数量 -->
<property name="maxActive" value="${jdbc_maxActive}" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="120" />
<!-- 最长等待时间,单位毫秒 -->
<property name="maxWait" value="3000" />
<property name="validationQuery" value="select 1 from dual" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="18000000" />
<property name="testOnBorrow" value="true" />
</bean>

<!-- mybatis文件配置,扫描所有mapper文件 --> 
<bean id="cfsqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:config/mybatis-config.xml" />
</bean>

<!-- spring与mybatis整合配置,扫描所有dao --> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.soft.dao" />
<!--
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
-->
<property name="sqlSessionFactoryBeanName" value="cfsqlSessionFactory" />
</bean>

<!-- 事务管理  -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 启用对事务注解的支持 -->
<tx:annotation-driven transaction-manager="transactionManager" /> 

<!-- ws配置 -->
<import resource="classpath:config/\webService.xml" />

</beans>


4.webService.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd ">

<import resource="classpath:META-INF/cxf/cxf.xml" />

<!-- 接口定义 -->
<jaxws:endpoint id="userService" implementor="com.soft.webservice.impl.UserWsImpl" address="/userWS">
</jaxws:endpoint>

</beans>


5.spring-mvc.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- 扫描controller(controller层注入) --> 
    <context:component-scan base-package="com.soft.controller"/> 

<!-- 开启注解 -->
<mvc:annotation-driven />

    <!--
配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能
-->
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/upload/**" location="/upload/" />

<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 异常解析器 -->
<bean id="simpleMappingExceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop
key="org.springframework.web.multipart.MaxUploadSizeExceededException">common/fileerror</prop>
</props>
</property>
</bean>

</beans>

6.mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!-- 实体类,简称 -设置别名 -->
<typeAliases>
<typeAlias alias="customer" type="com.soft.model.Customer" />
</typeAliases>

<!-- 实体接口映射资源 -->
<!--
说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,
因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml
-->
<mappers>
<mapper resource="com/soft/dao/impl/customerMapper.xml" />
</mappers>

</configuration> 

7. 接口
package com.soft.webservice;

import java.util.List;
import javax.jws.WebService;
import com.soft.model.Customer;

@WebService
public interface UserWs {

/**
* 查询用户信息
* @return
*/
public List<Customer> queryUserAll();

}

8.实现类
package com.soft.webservice.impl;

import java.util.List;
import javax.annotation.Resource;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.soft.dao.CustomerMapper;
import com.soft.model.Customer;
import com.soft.webservice.UserWs;

@WebService
public class UserWsImpl implements UserWs {

private Log logger = LogFactory.getLog(this.getClass());

@Resource
private CustomerMapper customerMapper;

/**
* 查询用户信息
* @return
*/
@Override
public List<Customer> queryUserAll() {
List<Customer> custList=null;
try{
custList=customerMapper.queryCustomerByParam();
}catch(Exception ce){
logger.error("query user all error",ce);
ce.printStackTrace();
}
return custList;
}

}