Spring配置事务管理器的两种方法(XML&annotation)

1.基于XML的配置

1.1 约束导入:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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">
</beans>

1.2 配置标签:

<!-- 配置数据源 -->
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
    <property name="username" value="zhou"></property>
    <property name="password" value="zhou"></property>
</bean>

<!-- 配置一个事务管理器 -->
<bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入 DataSource -->
        <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager"></tx:advice>

<!--在 tx:advice 标签内部 配置事务的属性 -->
<tx:attributes>
    <tx:method name="*" read-only="false" propagation="REQUIRED"/>
    <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>

<!-- 配置 aop -->
<aop:config>
    <!-- 配置切入点表达式 -->
    <aop:pointcut expression="execution(* com.xxx.service.impl.*.*(..))"id="pt1"/>
</aop:config>

<!-- 在 aop:config 标签内部: 建立事务的通知和切入点表达式的关系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>

2.基于注解的配置:

2.1 配置文件导入约束并配置扫描的包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置 spring 创建容器时要扫描的包 -->
    <context:component-scan base-package="com.xxx"></context:component-scan>
    <!-- 配置 JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置 spring 提供的内置数据源 -->
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"value="com.mysql.jdbc.Driver"></property>
        <property name="url"
    value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="username" value="zhou"></property>
        <property name="password" value="zhou"></property>
    </bean>
</beans>
<!-- 开启 spring 对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

2.2 核心注解

@Transactional注解 :配置在类上的话,类中的所有方法都被增强,如果配置在方法上,只作用于此方法的增强。
读写型事务配置(一般用于非查询方法)
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
只读型事务配置(一般用于查询方法)
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、事务简单介绍 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功。 1.1 事...
    tuacy阅读 116,885评论 11 110
  • 1.Spring事务管理的两种方式 Java EE应用的事务策略分为全局事务和局部事务。大多数情况下,我们都使用局...
    秃头哥编程阅读 618评论 1 0
  • 1.数据库事务基础知识 1.1.何为数据库事务 数据库事务的4个特性 原子性:组成一个事务的多个数据库操作是一个不...
    小螺钉12138阅读 1,602评论 1 18
  • 这部分的参考文档涉及数据访问和数据访问层和业务或服务层之间的交互。 Spring的综合事务管理支持覆盖很多细节,然...
    竹天亮阅读 1,053评论 0 0
  • 青春是一种病,给你既带来期待的喜悦,也给你带来说不清的伤痛,这种伤痛随着时间的流失,岁月的轮转,它有...
    侠客苇子阅读 381评论 2 2