详解Spring MVC事务配置

发布时间 - 2026-01-11 00:35:44    点击率:

要了解事务配置的所有方法,请看一下《Spring事务配置的5种方法》

本文介绍两种配置方法:

 一、XML,使用tx标签配置拦截器实现事务

二、Annotation方式

以下所使用环境为Spring4.0.3、Hibernate4.3.5

 一、 XML,使用tx标签配置拦截器实现事务

Entity类User.java,持久化类,对应数据库表user

package com.lei.demo.entity;

import javax.persistence.*;

@Entity(name="users")
public class Users {
  
  public Users(){
    super();
  }
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="id")
  private Integer id;
  
  @Column(name="user_name",length=32)
  private String user_name;
  
  @Column(name="age")
  private Integer age;
  
  @Column(name="nice_name",length=32)
  private String nice_name;
  
  //属性实现......

}

UserDAO.javar,表user的一些操作,其中属性sessionFactory应该由Spring注入,如下:

package com.lei.demo.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.lei.demo.entity.Users;

public class UsersDAO {
  private SessionFactory sessionFactory;

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

  public SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public List<Users> getAllUser(){
    String hsql="from users";
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hsql);
    
    return query.list();
  }
}

UserService.java,业务实现类,如下

package com.lei.demo.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lei.demo.dao.*;

public class UserService {
  private UsersDAO userDao;
  
  public int userCount(){
    return userDao.getAllUser().size();
  }

  public UsersDAO getUserDao() {
    return userDao;
  }

  public void setUserDao(UsersDAO userDao) {
    this.userDao = userDao;
  }

}

首先看一下xml配置,spring-hibernate.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    ">

  <!-- Hibernate4 -->
  <!-- 加载资源文件 其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
  <context:property-placeholder location="classpath:persistence-mysql.properties" />
  
  <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
      <list>
        <!-- 可以加多个包 -->
        <value>com.lei.demo.entity</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> 
      </props>
    </property>
  </bean>
  
  <!-- 数据库映射 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="${jdbc.driverClassName}" />
   <property name="url" value="${jdbc.url}" />
   <property name="username" value="${jdbc.user}" />
   <property name="password" value="${jdbc.pass}" />
  </bean>
  
  <!-- 配置Hibernate事务管理器 -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  
  <!-- 配置事务异常封装 -->
  <bean id="persistenceExceptionTranslationPostProcessor" 
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
  
  <!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED" />
      <tx:method name="get*" propagation="REQUIRED" />
      <tx:method name="*" read-only="true" />
    </tx:attributes>
  </tx:advice>
  
  <aop:config expose-proxy="true">
    <!-- 只对业务逻辑层实施事务 -->
    <aop:pointcut id="txPointcut" expression="execution(* com.lei.demo.service..*.*(..))" />
    <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
    <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
  </aop:config>
  
</beans>

其中主要配置中是tx:advice和aop:config两个配置节,以Spring AOP的方式实现事务管理。

tx:advice配置了事务的管理者是transactionManager,同时tx:method也规定了如果方法名匹配“add*”和“get*”方法时使用事务,propagation是设定事务的传播级别。除了“add*”和“get*”方法,其他的方法的事务是只读的(典型地,对于只执行查询的事务你会将该属性设为true,如果出现了更新、插入或是删除语句时只读事务就会失败)

aop:config指定了一个aop:pointcut去引用上边的advice。

这样就通过AOP的拦截机制实现了事务,当然你还要用Spring的方式自己配置UserDAO和UserService。

二、Annotation方式

第一步,首先看一下web.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:web="http://java.sun.com/xml/ns/javaee" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
 <display-name>Archetype Created Web Application</display-name>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/spring-*.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>lei-dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:/lei-dispatcher-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>lei-dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

第二步,spring-hibernate配置,见以下spring-hibernate.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    ">

  <!-- Hibernate4 -->
  <!-- 加载资源文件 其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
  <context:property-placeholder location="classpath:persistence-mysql.properties" />
  
  <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
      <list>
        <!-- 可以加多个包 -->
        <value>com.lei.demo.entity</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> 
      </props>
    </property>
  </bean>
  
  <!-- 数据库映射 -->
  <!-- class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" -->
  <!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="${jdbc.driverClassName}" />
   <property name="url" value="${jdbc.url}" />
   <property name="username" value="${jdbc.user}" />
   <property name="password" value="${jdbc.pass}" />
  </bean>
  
  <!-- 配置Hibernate事务管理器 -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  
  <!-- 配置事务异常封装 -->
  <bean id="persistenceExceptionTranslationPostProcessor" 
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

第一节中xml配置事务中需要通过配置tx:advice和aop:config来增加事务的功能。此处采用全注释方法,这两个配置节就不需要了。

 相应的需要在视图解析配置中启用注释,如下lei-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    ">
    
  <!-- 启动自动扫描 该包下所有的Bean(@Controller) -->
  <context:component-scan base-package="com.lei.demo" />
  
  <!-- 基于注释的事务,当注释中发现@Transactional时,使用id为“transactionManager”的事务管理器 -->
  <!-- 如果没有设置transaction-manager的值,则spring以缺省默认的事务管理器来处理事务,默认事务管理器为第一个加载的事务管理器 -->
  <tx:annotation-driven transaction-manager="transactionManager"/>
  
  <!-- 定义视图解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
      <value>/WEB-INF/user/</value>
    </property>
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>
  
</beans>

UserDAO如下

package com.lei.demo.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.lei.demo.entity.Users;

@Repository
public class UsersDAO {
  @Resource(name="sessionFactory")
  private SessionFactory sessionFactory;

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

  public SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public List<Users> getAllUser(){
    String hsql="from users";
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hsql);
    
    return query.list();
  }
}

UserService.java如下

package com.lei.demo.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lei.demo.dao.*;

@Service("userService")
public class UserService {
  @Resource
  private UsersDAO userDao;
  
  @Transactional
  public int userCount(){
    return userDao.getAllUser().size();
  }

  public UsersDAO getUserDao() {
    return userDao;
  }

  public void setUserDao(UsersDAO userDao) {
    this.userDao = userDao;
  }

}

这里,方法名userCount上加入@Transactional,说明这个方法要启用事务。如果类名UserService上加入@Transactional,则表明这个类中的所有方法都会启用事务。

如果配有多个transactionManager,例如配置有transactionManager1,和transactionManager2,则可以通过@Transactional(“transactionManager1”),的方式指定使用哪个数据源的事务。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Spring  # MVC事务配置  # springmvc配置事务管理 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: java中使用zxing批量生成二维码立牌  js实现点击每个li节点,都弹出其文本值及修改  Laravel怎么清理缓存_Laravel optimize clear命令详解  南京网站制作费用,南京远驱官方网站?  在线制作视频网站免费,都有哪些好的动漫网站?  制作公司内部网站有哪些,内网如何建网站?  EditPlus中的正则表达式实战(6)  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程  Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议  如何在万网开始建站?分步指南解析  Laravel怎么自定义错误页面_Laravel修改404和500页面模板  Laravel怎么实现支付功能_Laravel集成支付宝微信支付  Laravel Seeder填充数据教程_Laravel模型工厂Factory使用  专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?  HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】  Laravel项目怎么部署到Linux_Laravel Nginx配置详解  如何用y主机助手快速搭建网站?  阿里云网站搭建费用解析:服务器价格与建站成本优化指南  Laravel Fortify是什么,和Jetstream有什么关系  Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧  Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】  西安专业网站制作公司有哪些,陕西省建行官方网站?  如何快速生成专业多端适配建站电话?  百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧  javascript中的数组方法有哪些_如何利用数组方法简化数据处理  Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制  浅谈javascript alert和confirm的美化  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  如何续费美橙建站之星域名及服务?  小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?  在Oracle关闭情况下如何修改spfile的参数  简历没回改:利用AI润色让你的文字更专业  Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】  Laravel如何与Inertia.js和Vue/React构建现代单页应用  Laravel如何使用Sanctum进行API认证?(SPA实战)  Laravel的.env文件有什么用_Laravel环境变量配置与管理详解  如何挑选优质建站一级代理提升网站排名?  如何用AWS免费套餐快速搭建高效网站?  Claude怎样写约束型提示词_Claude约束提示词写法【教程】  如何利用DOS批处理实现定时关机操作详解  黑客如何通过漏洞一步步攻陷网站服务器?  桂林网站制作公司有哪些,桂林马拉松怎么报名?  php读取心率传感器数据怎么弄_php获取max30100的心率值【指南】  Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势  Laravel storage目录权限问题_Laravel文件写入权限设置  Win11怎么设置虚拟桌面 Win11新建多桌面切换操作【技巧】  如何彻底卸载建站之星软件?  Laravel如何实现API版本控制_Laravel API版本化路由设计策略  教你用AI将一段旋律扩展成一首完整的曲子