Spring03--Spring基于注解的AOP、Spring事务

本文介绍了Spring基于注解的AOP实现,包括@Aspect、@Before、@After等通知类型,以及如何在配置文件中启用AOP支持。同时,详细讲解了Spring事务管理,如PlatformTransactionManager接口、TransactionDefinition和TransactionStatus,以及如何配置事务管理器和事务属性。最后,给出了事务管理的配置示例和测试代码。
摘要由CSDN通过智能技术生成

Spring基于注解的AOP

一、基于注解的AOP介绍

  • AOP注解方式 和 XML方式完成的功能是一样,只是采用方式不同而已。
  • 将原有的XML方式使用注解注意替代

二、环境搭建

  • 使用Spring02中Spring基于配置文件的AOP的环境搭建

三、使用注解替换XML的配置步骤

1、通知类使用注解和使用@Aspect注解声明为切面

在这里插入图片描述

2、在增强的方法上使用注解配置通知

(1)@Before
  • 作用:把当前方法看成是前置通知
  • 属性
    • value:用于指定切入点表达式,还可以指定切入点表达式的引用。
 @Before("execution(public void com.etime.service.impl.AccountServiceImpl.transferAccount(..))")
    public void startTransaction() {
        try {
            System.out.println("启动事务");
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }    
(2) @AfterReturning
  • 作用:把当前方法看成是后置通知。
  • 属性
    • value:用于指定切入点表达式,还可以指定切入点表达式的引用
    @AfterReturning("execution(public void com.etime.service.impl.AccountServiceImpl.transferAccount(..))")
    public void commitTransaction() {
        try {
            System.out.println("提交");
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
(3)@AfterThrowing
  • 作用:把当前方法看成是异常通知。
  • 属性
    • value:用于指定切入点表达式,还可以指定切入点表达式的引用
    @AfterThrowing("execution(public void com.etime.service.impl.AccountServiceImpl.transferAccount(..))")
    public void rollbackTransaction() {
        try {
            System.out.println("回滚");
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
(4)@After
  • 作用:把当前方法看成是最终通知。
  • 属性
    • value:用于指定切入点表达式,还可以指定切入点表达式的引用
    @After("execution(public void com.etime.service.impl.AccountServiceImpl.transferAccount(..))")
    public void closeConnection() {
        try {
            System.out.println("释放资源");
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

3、在Spring 配置文件中开启 Spring 对注解AOP的支持

<!-- 开启 spring 对注解 AOP 的支持 -->
<aop:aspectj-autoproxy/>
  • 或者使用 在Spring 配置类中 使用注解@EnableAspectJAutoProxy开启对 注解AOP 的支持

在这里插入图片描述

4、环绕通知直接配置

  • @Around
  • 作用:把当前方法看成是环绕通知。
  • 属性
    • value:用于指定切入点表达式,还可以指定切入点表达式的引用。
    @Around("execution(public void com.etime.service.impl.AccountServiceImpl.transferAccount(..))")
    public Object transactionAround(ProceedingJoinPoint pjp) {
        Object result = null;
        try {
            Object[] args = pjp.getArgs();
            startTransaction();
            result = pjp.proceed(args);
            commitTransaction();
        } catch (Throwable throwable) {
            rollbackTransaction();
            throwable.printStackTrace();
        } finally {
            closeConnection();
        }
        return result;
    }

5、切入点表达式注解

在这里插入图片描述

Spring事务讲解

1、Spring中事务的API详解

(1)PlatformTransactionManager作用
  • PlatformTransactionManager是一个 接口
  • 它是 Spring 的事务管理器,它里面提供了我们常用的操作事务的方法。

在这里插入图片描述

**注意:**PlatformTransactionManager 是接口类型,不同的 Dao 层技术则有不同的实现类,

  • Dao 层技术是jdbc 或 mybatis 时:org.springframework.jdbc.datasource.DataSourceTransactionManager

  • Dao 层技术是hibernate时:org.springframework.orm.hibernate5.HibernateTransactionManager

(2)TransactionDefinition作用

TransactionDefinition 是事务的定义信息对象,里面有如下方法

在这里插入图片描述

事务隔离级别

设置隔离级别,可以解决事务并发产生的问题,如脏读、不可重复读和虚读。

事务隔离级别说明
ISOLATION_DEFAULT默认级别,归属下列某一种
ISOLATION_READ_UNCOMMITTED未提交读,可以读取未提交数据
ISOLATION_READ_COMMITTED已提交读,只能读取已提交数据,解决脏读问题(Oracle默认级别)
ISOLATION_REPEATABLE_READ可重复读,解决不可重复度问题(MySQL默认级别)
ISOLATION_SERIALIZABLE串行化,解决幻读(虚读)问题
事务传播行为
事务传播行为说明
REQUIRED如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)
SUPPORTS支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
MANDATORY使用当前的事务,如果当前没有事务,就抛出异常
REQUERS_NEW新建事务,如果当前在事务中,把当前事务挂起。
NOT_SUPPORTED以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
NEVER以非事务方式运行,如果当前存在事务,抛出异常
NESTED如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作
事务超时时间

默认值是-1,没有超时限制。如果有,以秒为单位进行设置。

是否是只读事务

建议查询时设置为只读。

(3)TransactionStatus作用

TransactionStatus 接口提供的是事务具体的运行状态,方法介绍如下。
在这里插入图片描述

真正干活: 事务管理器 会根据TransactionDefinition 定义的信息, 进行事务的管理。管理过程当中产生事务的状态保存在TransactionStatus当中。

2、环境搭建

沿用上一小章节的代码,修改AccountDaoImpl 具体使用使用Spring提供的JdbcTemplate模板对象实现

package com.etime.dao.impl;

import com.etime.dao.AccountDao;
import com.etime.entity.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Account getAccountByName(String name){
        Account account = null;
        String sql = "select * from account where name = ?";
        account = jdbcTemplate.queryForObject( sql, new BeanPropertyRowMapper<>(Account.class),name);
        return account;
    }

    public int update(Account account){
        int rows = 0;
        String sql = "update account set money = ? where id = ?";
        rows = jdbcTemplate.update(sql, account.getMoney(), account.getId());
        return rows;
    }
}

3、事务管理配置步骤

(1)配置事务管理器
    <bean id="dtm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"></property>
    </bean>
(2) 配置事务的通知引用事务管理器
<!--事务的配置-->
    <tx:advice id="d" transaction-manager="dtm">

    </tx:advice>
(3)在tx:advice中配置事务的属性
<!-- 
    指定方法名称:是业务核心方法
     read-only:是否是只读事务。默认 false,不只读。
     isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。
     propagation:指定事务的传播行为。
     timeout:指定超时时间。默认值为:-1。永不超时。
     rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。
     没有默认值,任何异常都回滚。
     no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,任何异常都回滚。
 -->
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
(4)配置 AOP 切入点表达式
<!--事务的aop增强-->
    <aop:config>
        <aop:pointcut id="point"
                      expression="execution(public void com.etime.service.impl.AccountServiceImpl.transferAccount(..))"/>
    </aop:config>
<!--在aop:config标签内部:建立事务的通知和切入点表达式的关系-->
<aop:advisor advice-ref="d" pointcut-ref="point"></aop:advisor>

完整的配置文件内容:

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

    <context:component-scan base-package="com.etime"></context:component-scan>
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <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="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds"></property>
    </bean>

    <bean id="dtm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"></property>
    </bean>

    <tx:advice id="d" transaction-manager="dtm">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="point"
                      expression="execution(public void com.etime.service.impl.AccountServiceImpl.transferAccount(..))"/>
        <aop:advisor advice-ref="d" pointcut-ref="point"></aop:advisor>
    </aop:config>

</beans>
(5)测试
package com.etime.test;

import com.etime.service.AccountService;
import com.etime.util.SpringConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.sql.SQLException;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application.xml"})
public class SpringTest {

    @Autowired
    private ApplicationContext context;

    @Test
    public void t01() throws SQLException {
        AccountService accountService = (AccountService) context.getBean("accountService");
        accountService.transferAccount("liudh", "fanbb" , 1000);
    }
}

iguration(locations = {“classpath:application.xml”})
public class SpringTest {

@Autowired
private ApplicationContext context;

@Test
public void t01() throws SQLException {
    AccountService accountService = (AccountService) context.getBean("accountService");
    accountService.transferAccount("liudh", "fanbb" , 1000);
}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸鱼不咸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值