您的位置:首页 > 编程语言 > Java开发

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

2015-12-08 21:23 786 查看
项目框架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,问题解决!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息