我的配置如下<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd"> <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhoust:1521:ES1">
</property>
<property name="username" value="FWT"></property>
<property name="password" value="mcse"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
    <property name="mappingLocations">
<list>
<value>/**/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect = org.hibernate.dialect.Oracle9Dialect
hibernate.hbm2ddl.auto = update
hibernate.show_sql = true
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!-- <bean id="autoproxy"-->
<!--      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">-->
<!-- <property name="beanNames">-->
<!-- <list>-->
<!-- <value>*serviceimp</value>-->
<!-- </list>-->
<!-- </property>-->
<!-- <property name="interceptorNames">-->
<!-- <list>-->
<!-- <value>transactionInterceptor</value>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean>-->

<!-- 用户管理-->
<bean id="account" class="com.FNL.FWT.yonghu.dao.Imp.AccountDAOImp">
    <property name="sessionFactory">
       <ref bean="sessionFactory"/>
   </property>
</bean>
  <!-- 角色管理-->
<bean id="role" class="com.FNL.FWT.yonghu.dao.Imp.RoleDAOImp">
    <property name="sessionFactory">
       <ref bean="sessionFactory"/>
   </property>
</bean>
 <!--用户角色-->
<bean id="roleuser" class="com.FNL.FWT.yonghu.dao.Imp.RoleUserDAOImp">
     <property name="sessionFactory">
         <ref bean="sessionFactory"/>
     </property>
</bean>
<!--汇总报表及计划表导出-->
    <bean id="tomonthimp" class="com.FNL.FWT.jihuafenpei.dao.Imp.MonthDetailImp" 
           autowire="byType" />
<bean id="toexcelimp" class="com.FNL.FWT.jihuafenpei.dao.Imp.TotalDAOImp"
autowire="byType" />
   <bean name="/toexcel" class="com.FNL.FWT.jihuafenpei.struts.action.TotalExAction">
        <property name="toexcelimp" ref="toexcelimp"/>
        <property name="tomonthimp" ref="tomonthimp"/>
   </bean>
<!-- 线路省份dwr-->
<bean id="province" class="com.FNL.FWT.yunliluru.dao.Imp.Xl_ProvinceImp"
autowire="byType" />
<!-- 运力录入dwr模块 -->
<bean id="itransportinforimp" class="com.FNL.FWT.yunliluru.dao.Imp.TransportinforImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transportinforserviceimp" class="com.FNL.FWT.yunliluru.service.Imp.TransportinforServiceImp">
<property name="itransportinforimp" ref="itransportinforimp"/>
</bean>
<bean id="findtransportinformess" class="com.FNL.FWT.until.FindTransportinforMess">
<property name="transportinforserviceimp" ref="transportinforserviceimp"/>
<property name="fwtfenpeiserviceimp" ref="fwtfenpeiserviceimp"/>
</bean>

<!-- 当日计划录入dwr模块 -->
<bean id="customerinforimp" class="com.FNL.FWT.jihualuru.dao.Imp.CustomerinforImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="customerinforserviceimp" class="com.FNL.FWT.jihualuru.service.Imp.CustomerinforServiceImp">
<property name="customerinforimp" ref="customerinforimp"/>
</bean>
<bean id="findcustomerinformation" class="com.FNL.FWT.until.FindCustomerinformation">
<property name="customerinforserviceimp" ref="customerinforserviceimp"/>
</bean>
  <!-- 运力管理-->
   <bean id="ylmanagerimp" class="com.FNL.FWT.yunliluru.dao.Imp.YlManagerImp">
       <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <bean name="/ylmanager" class="com.FNL.FWT.yunliluru.struts.action.YlManagerAction">
       <property name="ylmanagerimp" ref="ylmanagerimp"></property>
   </bean>
   <!-- 公司信息管理-->
    <bean name="/transportinfo" class="com.FNL.FWT.yunliluru.struts.action.TransportInforAction1">
<property name="transportinforserviceimp" ref="transportinforserviceimp"/>
</bean> <!-- 当日运力录入模块 -->
<bean id="adddangriyunliimp" class="com.FNL.FWT.yunliluru.dao.Imp.AddDangriyunliImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="adddangriyunliserviceimp" class="com.FNL.FWT.yunliluru.service.Imp.AddDangriyunliServiceImp">
<property name="adddangriyunliimp" ref="adddangriyunliimp"/>
</bean>
<bean name="/danriyunli" class="com.FNL.FWT.yunliluru.struts.action.TransportinforAction">
<property name="adddangriyunliserviceimp" ref="adddangriyunliserviceimp"/>
<property name="addjihuayitianyunliserviceimp" ref="addjihuayitianyunliserviceimp"/>
</bean>

<!-- 计划运力录入模块 -->
<bean id="addjihuayitianyunliimp" class="com.FNL.FWT.yunliluru.dao.Imp.AddJihuayitianyunliImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="addjihuayitianyunliserviceimp" class="com.FNL.FWT.yunliluru.service.Imp.AddJihuayitianyunliServiceImp">
<property name="addjihuayitianyunliimp" ref="addjihuayitianyunliimp"/>
</bean>
<bean name="/jihuayitianyunli" class="com.FNL.FWT.yunliluru.struts.action.JihuayitianyunliAction">
<property name="addjihuayitianyunliserviceimp" ref="addjihuayitianyunliserviceimp"/>
</bean>

<!-- 当日计划录入 -->
<bean name="/jihualuru" class="com.FNL.FWT.jihualuru.struts.action.JihualuruAction">
<property name="customerinforserviceimp" ref="customerinforserviceimp"/>
</bean>
<!-- 客户信息维护 -->
<bean name="/customerinFor" class="com.FNL.FWT.jihualuru.struts.action.CustomerinForAction">
<property name="customerinforserviceimp" ref="customerinforserviceimp"/>
</bean>
<!--计划分配  -->
<bean id="fwtfenpeiimp" class="com.FNL.FWT.jihuafenpei.dao.Imp.FwtFenpeiImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="fwtfenpeiserviceimp" class="com.FNL.FWT.jihuafenpei.service.Imp.FwtFenpeiServiceImp">
<property name="fwtfenpeiimp" ref="fwtfenpeiimp"/>
</bean>
<bean name="/jihuafenpei" class="com.FNL.FWT.jihuafenpei.struts.action.JihuafenpeiAction">
<property name="customerinforserviceimp" ref="customerinforserviceimp"/>
<property name="fwtfenpeiserviceimp" ref="fwtfenpeiserviceimp"/>
</bean>
<!-- 相关表报打印 -->
<bean name="/excelDao" class="com.FNL.FWT.Excel.struts.action.ExcelDaoAction">
<property name="customerinforserviceimp" ref="customerinforserviceimp"/>
<property name="fwtfenpeiserviceimp" ref="fwtfenpeiserviceimp"/>
<property name="addjihuayitianyunliserviceimp" ref="addjihuayitianyunliserviceimp"/>
<property name="adddangriyunliserviceimp" ref="adddangriyunliserviceimp"/>
<property name="transportinforserviceimp" ref="transportinforserviceimp"/>
</bean>
</beans>
我还有个问题,代理Service层为什么比代理Dao层更好呢?我现在有很多Service类,有很多Dao类。我要更新两张表数据,并且在不同Service层怎样去控制同时成功同时失败?我觉得spring是不是只能判断在处理一张表的时候有效?多个表就识别不了?不像Hibernate那样控制那么灵活?

解决方案 »

  1.   

    添加一个HibernateTemplate的配置.
      

  2.   

    建议你去下一个Appfuse的源码看看,也许会有收获~~
      

  3.   

    service层的一个方法一般代表了一个完整的事务。
    想让2个表的操作在同一个事务当然可以啊,你把事务配置在service层,然后
    serviceA的方法里{
       daoB.addB();
       daoC.addC()
    }
    这样addB,addC就在同一个事务里了啊
      

  4.   

    你说的配置以后抛出异常不回滚的问题,是你没有配置好吧
    给你一个例子-----------------------------------------
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>
    <!-- 配置哪些方法需要哪些事务 -->
        <tx:advice id="txadvice" transaction-manager="transactionManager">
         <tx:attributes>
         <tx:method name="add*" propagation="REQUIRED"/>
         <tx:method name="remove*" propagation="REQUIRED"/>
         <tx:method name="update*" propagation="REQUIRED"/>
         <tx:method name="*" read-only="true"/>
         </tx:attributes>
        </tx:advice>
        <!-- service层应用事务 -->
        <aop:config>
         <aop:pointcut id="managerMethods" expression="execution (* com.spread.forum.business.service.*.*(..))"/>
         <aop:advisor advice-ref="txadvice" pointcut-ref="managerMethods"/>
        </aop:config>另外你测试的时候throw 一个Exception,是不会回滚的,spring默认回滚继承自RuntimeException的异常,你可以在
     <tx:method name="add*" propagation="REQUIRED" rollback-for="-Exception" />指定需要回滚的异常