当前位置: 代码迷 >> Web前端 >> Spring2.5与DWR使用中遇到的事务有关问题
  详细解决方案

Spring2.5与DWR使用中遇到的事务有关问题

热度:991   发布时间:2013-03-25 15:43:04.0
Spring2.5与DWR使用中遇到的事务问题

?在SSH中添加事务,代码如下:

<bean id="transactionIntercepter" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
        <prop key="cim*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
??? <property name="beanNames">
??????? <list>
??????????? <value>addproService</value>
??????????? <value>queryproServise</value>
??????????? <value>examservice</value>
??????????? <value>measureService</value>
??????? </list>
??? </property>
??? <property name="interceptorNames">
??????? <list>
??????????? <value>transactionIntercepter</value>
??????? </list>
??? </property>
</bea

其中 measureSrvice 要用到事务。

MeasureService的配置如下:

<?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.5.xsd">
    <!-- dao -->
    <bean id="measureDao" class="com.chengxin.measure.dao.impl.MeasureDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- service -->
    <bean id="measureService" class="com.chengxin.measure.service.impl.MeasureServiceImpl">
        <property name="dao" ref="measureDao" />
    </bean>
    <!-- action -->
    <bean id="measureAction" class="com.chengxin.measure.action.MeasureAction" scope="prototype">
        <property name="service" ref="measureService"></property>
        <property name="typeService" ref="typeservice"></property>      
    </bean>
</beans

?DWR相关measure配置如下 :

<create creator="spring" javascript="measure">
    <param name="beanName" value="measureService"></param>
    <include method="checkPunish" />
    <include method="checkNotify" />
    <include method="exists" />
</create>

?此时,页面上执行JavaScript脚本代码:measure.exists时总是弹出“ERROR”提示框。

网上百度下,在DWR配置中加上错误信息提示:

<convert converter="spring" match="java.lang.StackTraceElement" />
<convert converter="exception" match="java.lang.Exception" />

?再次执行,提示“Object is not an instance of declaring class"。

再次百度,大意是measureService由代理生成,DWR调用的时候与声明的类不匹配,所以出错。要修改Spring-bean的配置文件,修改如下:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <!-- dao -->
    <bean id="measureDao" class="com.chengxin.measure.dao.impl.MeasureDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- service -->
    <bean id="measureService" class="com.chengxin.measure.service.impl.MeasureServiceImpl">
        <aop:scoped-proxy/>
        <property name="dao" ref="measureDao" />
    </bean>
    <!-- action -->
    <bean id="measureAction" class="com.chengxin.measure.action.MeasureAction" scope="prototype">
        <property name="service" ref="measureService"></property>
        <property name="typeService" ref="typeservice"></property>      
    </bean>
</beans>

?再次执行,OK。

  相关解决方案