欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ssm整合后配置的事务不起作用的原因和解决方法

程序员文章站 2022-07-12 14:10:11
...

在完成ssm整合后,配置了事务,但是发现事务没有起作用

spring的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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 开启注解扫描 -->
	<context:component-scan
		base-package="com.bailiban" />
	<!-- 加载配置文件 -->
	<context:property-placeholder
		location="classpath:db.properties" />
	<!--注入数据源spring整合mybatis -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- 配置SqlSessionFactory工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<!-- 取别名 -->
	<property name="typeAliasesPackage" value="com.bailiban.pojo"></property>
	<!-- 加载mybatis配置文件 -->
	<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
	</bean>
	<!-- 配置扫描包 -->
	<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.bailiban.mapper"/>
	</bean>
	<!-- 注入事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 开启事务 -->
	<tx:annotation-driven />
</beans>

springmvc的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="com.bailiban"/>
        <!-- 配置视图解析器 -->
        <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
          <property name="suffix" value=".jsp"/>
        </bean>
        <!-- 静态资源不拦截 -->
        <mvc:resources location="/css/" mapping="/css/**"/>
         <mvc:resources location="/js/" mapping="/js/**"/>
          <mvc:resources location="/images/" mapping="/images/**"/>
          <!--  加载驱动-->
          <mvc:annotation-driven/>
</beans>

代码结构如下图:

ssm整合后配置的事务不起作用的原因和解决方法

测试事务代码,在Service业务层加上如下异常

ssm整合后配置的事务不起作用的原因和解决方法 

最终测试,发现抛出异常,但是事务并没有回滚,成功保存!

原因 :springmvc和spring的配置扫描包,均是扫描所有com.bailiban 下面的包,而springmvc只需要扫描Controller层,即com.bailiban.controller即可。

具体解释如下:(摘自:引用

1、Spring与SpringMVC属于父子容器关系。框架启动时先启动Spring容器,而后启动SpringMVC容器。子容器可以访问父容器中的Bean,而父容器不能访问子容器中的Bean。

2、由于SpringMVC在扫描时扩大了扫描范围,装载了@Service标识的类的实例,从而导致Controller层在注入Service时,实际注入的时子容器中的Service实例。

3、事务被配置在父容器中,Spring父容器在装载Service时会同时应用事务配置,而SpringMVC只是单纯加载Service的实例。

解决方法: 将springmvc中的扫描范围缩小到controller层即可

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="com.bailiban.controller"/>
        <!-- 配置视图解析器 -->
        <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
          <property name="suffix" value=".jsp"/>
        </bean>
        <!-- 静态资源不拦截 -->
        <mvc:resources location="/css/" mapping="/css/**"/>
         <mvc:resources location="/js/" mapping="/js/**"/>
          <mvc:resources location="/images/" mapping="/images/**"/>
          <!--  加载驱动-->
          <mvc:annotation-driven/>
</beans>

 

相关标签: spring