• Spring声明式事务管理基于@Transactional注解


    概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解
             第一种方式我已在上文为大家讲解了,那接下来我为大家讲解一下基于@Transactional 注解Spring声明式事务管理;

    首先为大家介绍一下它的用法:

          @Transactional 注解可以作用于接口、接口方法、类以及类方法上。当作用于类上时,该类的所有 public 方法 将都具有该类型的事务属性,同时,我们也可以在方法级别使用该标注来覆盖类级别的定义。

         虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是建议不要在接口或者接口 方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional 注解应该只被应用 到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上 使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。

        默认情况下,只有来自外部的方法调用才会被AOP代理捕获,也就是,类内部方法调用本类内部的其他方法 并不会引起事务行为,即使被调用方法使用@Transactional 注解进行修饰。

    接着在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:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="    
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
        <!-- 原理:自动注入processor解析器,用来解析注解 -->
        <!-- <context:annotation-config/> -->
    
        <!-- 自动扫描包,也会自动注入解释器,所以不需要 context:annotation-config -->
        <context:component-scan base-package="news"></context:component-scan>
    
    
    
        <!-- 引入外部属性文件 -->
        <context:property-placeholder location="classpath:jdbc.properties" />
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
            <property name="dataSource" ref="myDataSource" />
    
            <!-- 配置Hibernate的其他的属性 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.connection.autocommit">false</prop>
                    <!-- 开机自动生成表 -->
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            <property name="mappingResources">
                <list>
                    <value>news/entity/News.hbm.xml</value>
                </list>
            </property>
    
        </bean>
    
        <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.url}" />
            <property name="user" value="${jdbc.user}" />
            <property name="password" value="${jdbc.password}" />
            <!-- 每300秒检查所有连接池中的空闲连接 -->
            <property name="idleConnectionTestPeriod" value="300"></property>
            <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
            <property name="maxIdleTime" value="900"></property>
            <!-- 最大连接数 -->
            <property name="maxPoolSize" value="2"></property>
    
        </bean>
    
       <!-- 配置spring的TransactionManager,名字为默认值 -->  
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
             
       <!-- 开启事务控制的注解支持 --> 
        <!--用@Transactional 注解替代tx/aop命名空间  来实现 Spring声明式事务管理-->
        <!--在service类上使用注解@Transactional -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
    
    </beans>

    当作用于类上时,该类的所有 public 方法将都具有该类型的事务属性,同时,我们也可以在方法级别使用该注解来覆盖类级别的定义。

    //在service类上的注解
    @Transactional
    @Service
    @Scope("prototype")
    public class NewsServiceImpl implements NewsService {
            
        @Autowired
        private NewsDao nd;
         
        @Override
        @Transactional(readOnly=true)  //为方法增加事务处理特性
    public List showAllNews() {

    List
    <News> allNewList = nd.showAllNews();
    return allNewList;
    }
     

    总结:通过对基于tx/aop命名空间的xml配置文件基于@Transactional 注解这两种声明式事务管理方式的使用 ,我们可以清楚的了解到,注解和 Java 代码位

    于一个文件中,而 XML 配置采用独立的配置文件,大多数配置信息在 程序开发完成后都不会调整,如果配置信息和 Java 代码放在一起,有助于增强程序的内聚性。而

    采用独立的 XML 配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。因此在很多情况下,注解配

    置比XML 配置更受欢迎。但是XML配置文件方式也有它一定的优点,所以具体该使用哪种方式还得依据具体情况分析。

  • 相关阅读:
    tensorflow2.0第1章 Tensorflow简介与环境搭建
    SIGAI机器学习第二十四集 聚类算法1
    SIGAI机器学习第二十三集 高斯混合模型与EM算法
    51nod1429 巧克力
    CTSC2018 Day2T1 Juice混合果汁
    CF1B Spreadsheets
    CF2B The least round way
    【模板】点分治
    【模板】AC自动机
    【模板】网络流-最大流 Dinic
  • 原文地址:https://www.cnblogs.com/liguanxing/p/5992969.html
Copyright © 2020-2023  润新知