ShiroDbRealm 导致spring 事务配置无效原因分析

项目框架springMVC + spring + mybatis,配置的事务是基于@transation注解的,最近发现有的模块事务有作用,有的事务没有生效,

spring 配置如下:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
       default-lazy-init="true">
    <description>Spring公共配置</description>
    <tx:annotation-driven proxy-target-class="true"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!-- 使用annotation 自动注册bean, 并保证@Required@Autowired的属性被注入 -->
    <context:component-scan base-package="com.zeustel" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 读取配置文件  ignore-unresolvable true时可以配置多个context:property-placeholder加载多个properties配置文件-->
   <context:property-placeholder location="classpath*:/db.properties" ignore-unresolvable="false"/>

    <!--  c3p0数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations">
            <list>
                <value>classpath*:**/sql/*Mapping.xml</value>
            </list>
        </property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zeustel.dao.mapper" />
    </bean>

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

     <!-- 全局异常配置 -->
     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
         <property name="exceptionMappings">
             <props>
                 <prop key="java.lang.Exception">errors/500</prop>
                 <prop key="java.lang.Throwable">errors/500</prop>
             </props>
         </property>
         <property name="statusCodes">
             <props>
                 <prop key="errors/500">500</prop>
             </props>
         </property>
         <!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->
         <property name="warnLogCategory" value="WARN"></property>
         <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->
         <property name="defaultErrorView" value="errors/500"></property>
         <!-- 默认HTTP状态码 -->
         <property name="defaultStatusCode" value="500"></property>
     </bean>
</beans>

尝遍了网上所有方法,有的说spring-mvc.xml 和application-context..xml 扫描包原因, 有的说spring aop和事务先后顺序问题,还有aop 使用cglib 的问题,还有数据库表引擎的问题等等。。。。   还是一样,突然想到了再 单元测试环境下试试 事务效果,

一开始单元测试类配置如下

 把之前事务不生效的service 加上,一运行,居然可以啦!

还是很疑惑,为什么web容器下就不行呢, 继续在网上找答案,也没找到相关的,

忽然想到web容器和junit 环境配置的不通,就把web容器改加载的配置文件一个个 加上再运行,  后来发现了再加上

classpath*:spring-mvc.xml
事务也可以生效, 排除这两个文件的问题, 最后找到 shiro的配置文件 加上去

发现事务又不行了, 这时喜出望外,确定都是 shiro惹的祸! 网上一查, 原来是自定义的ShiroDbRealm 里面注入的service 引起的

后来把 ShiroDbRealm里面注入 service改为注入dao,问题解决!





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值