声明注解式事务

参考错误解决:
终于找到全annotation配置springMVC的方法了(事务不失效): [url]http://icanfly.iteye.com/blog/778401[/url]
解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题:
[url]http://fengzhiyin.iteye.com/blog/714686[/url]
留言下面写到:[color=red]mvc 的只扫描controller组件 注意使用 use-default-filters="false" [/color]
<context:component-scan base-package="com.fengzhiyin" use-default-filters="false" >
<context:[color=red]include-filter[/color] expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

主体的扫描除controller外的所有组件
<context:component-scan base-package="com.fengzhiyin" >
<context:[color=red]exclude-filter[/color] expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

产生问题:
java.lang.NoClassDefFoundError: org/objectweb/asm/Type:
[url]http://blog.csdn.net/apple90/article/details/2968658[/url]
java.lang.NoSuchMethodError: org.objectweb.asm.org.objectweb.asm.ClassWriter.:
[url]http://wxpwdm8461.iteye.com/blog/586575[/url]


同时,[color=red]抛出异常应该是RuntimeException类型的异常[/color],否则事务失败。
throw new RuntimeException("@@@@@@@@@@@@@@@@@@ RollBack...............");


配置原则:
[color=red][b]spring-servlet.xml只负责扫描controller类。
applicationContext.负责扫描controller以外的类。[/b][/color]
因为spring的context是父子容器,所以会产生冲突,Controller会先进行扫描装配,而此时的Service还没有进行事务的增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描配置进行事务处理。
[color=red]xml被扫描顺序参考[/color]: [url]http://sence-qi.iteye.com/blog/1328902[/url]
由于服务器启动时的加载配置文件的顺序为web.xml---root-context.xml(Spring的配置文件)---servlet-context.xml(SpringMVC的配置文件),由于root-context.xml配置文件中Controller会先进行扫描装配,但是此时service还没有进行事务增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力)

配置文件:
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: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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byName" default-lazy-init="true">

<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- <context:component-scan base-package="com.">
<context:exclude-filter type="regex" expression=".*Controller$" />
</context:component-scan> -->

<context:component-scan base-package="com" >
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;DatabaseName=UNIT_TEST_CASE_SYS">
</property>
<property name="username" value="sa"></property>
<property name="password" value="asl12345"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
com.common.SqlServer2008Dialect
</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.pojo.Modules</value>
<value>com.pojo.TestCaseDetails</value>
<value>com.pojo.RoundDetails</value>
<value>com.pojo.Projects</value>
<value>com.pojo.Users</value>
<value>com.pojo.TestCases</value>
<value>com.pojo.Functions</value>
<value>com.pojo.UserType</value>
<value>com.pojo.Rounds</value>
<value>com.pojo.ProjectBrowers</value>
</list>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</bean>
</beans>


<property name="annotatedClasses">
<list>
<value>com.pojo.Modules</value>
<value>com.pojo.TestCaseDetails</value>
<value>com.pojo.RoundDetails</value>
<value>com.pojo.Projects</value>
<value>com.pojo.Users</value>
<value>com.pojo.TestCases</value>
<value>com.pojo.Functions</value>
<value>com.pojo.UserType</value>
<value>com.pojo.Rounds</value>
<value>com.pojo.ProjectBrowers</value>
</list>
</property>

可以让
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:packagesToScan="com.pojo" >

去配置,就不用每个都写一句。


spring-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<mvc:annotation-driven />
<!-- <context:component-scan base-package="com.service.impl" />
<context:component-scan base-package="com.dao.impl" />
<context:component-scan base-package="com.pojo" />
<context:component-scan base-package="com.controller" /> -->
<context:component-scan base-package="com" use-default-filters="false" >
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值