论坛首页 Java企业应用论坛

CXF(前身xfire) 整合 spring 以及 aop事务配置

浏览 6208 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2009-02-04  

说明此文档是整合cxf与spring的,如果需要整合hibernate的话,与整合spring+hibernate是一样的,不需要考虑的。
另外,在此项目中我们通常使用的事务我也将在这里进行配置,是通过spring aop实现的,比较简单,但是很实用,本来想将aop统一异常处理也给贴上来的,但是感觉配置的并不完美,所以也就不向上写了,等我在下一个教程中在贴出来的。
首先建立一个web工程 我的教程是从我项目中直接拷贝出来的,所以也就没有进行修改,直接粘贴上来了,工程名字请自己随意建。
手动添加spring包,然后手动将cxf的包拷贝到lib中,在这里需要提一下,拷贝的包需要进行过滤,否则使用aop的时候会报错,项目无法启动,再倒入的时候需要将与spring冲突的包删除掉,我会在最后附上我所用到的包。
如果在启动时候报错了,一般情况就是包冲突造成的,只需要将两个框架中的包相对应一下,找出重复的删除掉即可。
下面是我的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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://cxf.apache.org/jaxws 
http://cxf.apache.org/schemas/jaxws.xsd ">

<!-- 数据库模块 初始化 -->
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
   <property name="url" value="jdbc:sqlserver://192.168.234.250:1433;databaseName=fy_erp_v6"></property>
   <property name="username" value="sa"></property>
</bean>
<!-- 配置同一个数据源 -->
<bean id="daoTemplate" abstract="true" lazy-init="true">
   <property name="dataSource" ref="DataSource" />
</bean>
<!-- 事务处理aop -->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 使用spring jdbc 事务管理。 -->
   <property name="dataSource" ref="DataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="TransactionManager">
   <tx:attributes>
    <tx:method name="save*" read-only="false" rollback-for="Exception"/><!-- 如果系统抛出异常,事务回滚。 -->
    <tx:method name="update*" read-only="false" rollback-for="Exception"/>
    <tx:method name="delete*" read-only="false" rollback-for="Exception"/>
    <tx:method name="get*" read-only="true"/>
   </tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
   <aop:pointcut id="DynamicDataServicePointcut" expression="execution(* *..*Service.*(..))"/><!-- 配置只要以service结尾的接口都将通过事务管理 -->
   <aop:advisor advice-ref="txAdvice" pointcut-ref="DynamicDataServicePointcut"/>
</aop:config>
<!-- 事务处理aop -->
<!-- 数据库模块 初始化 --> 

<!-- service 服务 bean begin -->
<bean id="IVipService" class="com.wfy.sa.webinterface.service.impl.VipServiceImpl">
   <property name="vipDao" ref="VipDao" />
</bean>
<!-- service 服务 bean end -->
<!-- Dao 数据层 bean begin -->
<bean id="VipDao" class="com.wfy.sa.webinterface.dao.VipDao" parent="daoTemplate"/>
<!-- Dao 数据层 bean end -->

<!-- webService相关配置 begin -->
<!-- webService 服务 bean begin -->
<bean id="IFyErpWebService" class="com.wfy.sa.webinterface.impl.FyErpWebServiceImpl">
   <property name="vipService" ref="IVipService" />
</bean>
<!-- webService 服务 bean end -->
<!-- cxf webservice 服务 配置 begin -->
<!-- 引入cxf的bean定义文件 -->
<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"/>
<!-- 暴露我的webservice服务 -->
<!--
这里是cxf的重点,就是通过这里将接口暴露出去的
address="/FyErpWebService" 
是接口暴露的地址,按照我的配置,地址应该是 http://localhost:8080/fy_erp_webService/wfyErpWebService/FyErpWebService?wsdl
这个地址就是将来我们提供给用户访问的接口地址。fy_erp_webService这是我的工程名字,wfyErpWebService是我webservice的路径。
-->
<jaxws:server id="FyErpWebService" serviceClass="com.wfy.sa.webinterface.IFyErpWebService" address="/FyErpWebService">
   <jaxws:serviceBean>
    <ref bean="IFyErpWebService"/><!-- 要暴露的 bean 的引用 -->
   </jaxws:serviceBean>
</jaxws:server>
<!-- cxf webservice 服务 配置 end -->
<!-- webService相关配置 end -->
</beans>

 

下一步贴出我的web.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
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_2_5.xsd">

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- cxf webservice 服务 servlet -->
<servlet>
   <servlet-name>CXFServlet</servlet-name>
   <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
<!-- CXFServlet Mapping -->
<servlet-mapping>
   <servlet-name>CXFServlet</servlet-name>
   <url-pattern>/wfyErpWebService/*</url-pattern><!-- 这里配置了webservice访问的路径。 -->
</servlet-mapping>
</web-app>

 

剩下dao层与业务逻辑层的代码就不贴了,因为我里面并没有写代码,我就贴上我的webservice层的代码:
接口:

package com.wfy.sa.webinterface;

import javax.jws.WebService;

@WebService//这个一定要写上,否则webservice访问不到的。
public interface IFyErpWebService {
public String getItemInfo(String key, String value);
public int getVipIntegral(String vipNo);
public String getVipSaleInfo(String vipNo);
public boolean setSaleInfo(String clientNo, String saleInfo);
}

 

实现:

package com.wfy.sa.webinterface.impl;

import javax.jws.WebService;

import com.wfy.sa.webinterface.IFyErpWebService;
import com.wfy.sa.webinterface.service.IProductService;
import com.wfy.sa.webinterface.service.IVipService;

@WebService//这个一定要写上,否则webservice访问不到的。
public class FyErpWebServiceImpl implements IFyErpWebService {
private IVipService vipService;//业务逻辑层的注入信息
public void setVipService(IVipService vipService) {
   this.vipService = vipService;
}

@Override
public String getItemInfo(String key, String value) {
   // TODO Auto-generated method stub
   return null;
}

@Override
public int getVipIntegral(String vipNo) {
   // TODO Auto-generated method stub
   return 0;
}

@Override
public String getVipSaleInfo(String vipNo) {
   // TODO Auto-generated method stub
   return null;
}

@Override
public boolean setSaleInfo(String clientNo, String saleInfo) {
   // TODO Auto-generated method stub
   return false;
}
}

 

这样部署项目,启动服务,如无以为将会成功启动。
如果依然失败,请将错误信息贴上来我给大家解答。
我在系统中所用的所有的包:

<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/easymock.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/easymockclassextension.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/freemarker.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-j2ee-connector_1.5_spec-1.0.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-jpa_3.0_spec-1.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-jta_1.0.1B_spec-1.0.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/groovy-1.5.5.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hessian-3.1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/iText-2.0.7.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ibatis-2.3.0.677.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jamon-2.4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jasperreports-2.0.5.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jdo2-api.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jmxremote.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jmxremote_optional.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jmxri.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jotm.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jruby.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/junit-4.4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jxl.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/openjpa-0.9.7-incubating.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/persistence.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/poi-3.0.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/portlet-api.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/quartz-all-1.6.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/serp-1.11.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-agent.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aop.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aspects.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-beans.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-binding-1.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-support.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-core.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-javaconfig.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jdbc.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jms.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-orm.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-test.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-tomcat-weaver.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-tx.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-web.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webflow-1.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-portlet.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-struts.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/toplink-essentials-agent.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/toplink-essentials.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/velocity-1.5.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/velocity-tools-view-1.4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xapool.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/sqljdbc.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/FastInfoset-1.2.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/XmlSchema-1.4.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cxf-2.1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cxf-manifest.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-activation_1.1_spec-1.0.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-annotation_1.0_spec-1.1.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-javamail_1.4_spec-1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-jaxws_2.1_spec-1.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-jms_1.1_spec-1.1.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-servlet_2.5_spec-1.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-stax-api_1.0_spec-1.0.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-ws-metadata_2.0_spec-1.1.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jaxb-api-2.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jaxb-impl-2.1.7.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jaxb-xjc-2.1.7.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jaxen-1.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jdom-1.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jettison-1.0.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jetty-6.1.9.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jetty-util-6.1.9.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jra-1.0-alpha-4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/js-1.6R7.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jsr311-api-0.8.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/neethi-2.0.4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/opensaml-1.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/oro-2.0.8.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/saaj-api-1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/saaj-impl-1.3.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/serializer-2.7.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-api-1.3.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-jdk14-1.3.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/wsdl4j-1.6.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/wss4j-1.5.4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/wstx-asl-3.2.6.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xalan-2.7.1.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xml-resolver-1.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xmlbeans-2.3.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xmlsec-1.4.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/abdera-core-0.4.0-incubating.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/abdera-extensions-json-0.4.0-incubating.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/abdera-extensions-main-0.4.0-incubating.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/abdera-i18n-0.4.0-incubating.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/abdera-parser-0.4.0-incubating.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ant-1.6.5.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr-2.7.7.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aopalliance-1.0.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aopalliance.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-2.2.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-commons-2.2.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-util-2.2.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aspectjlib.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aspectjrt.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aspectjweaver.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/axiom-api-1.2.7.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/axiom-impl-1.2.7.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/backport-util-concurrent.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/bcprov-jdk15-140.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/bsh-2.0b4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/c3p0-0.9.1.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cglib-nodep-2.1_3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commonj-twm.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-attributes-api.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-attributes-compiler.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-codec-1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-codec.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-collections-3.2.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-dbcp.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-fileupload.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-httpclient.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-io.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang-2.4.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-pool-1.3.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-pool.jar"/>

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics