Spring AOP的应用

声明式事务

XML配置方式

配置事务管理器

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

配置AOP通知(advice)

简单示例:
<!-- 示例演示给所有get、find、query开头的方法添加只读事务,其他方法是默认事务(可读可写) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
   <tx:attributes>
       <tx:method name="get*" read-only="true" />
       <tx:method name="find*" read-only="true" />
       <tx:method name="query*" read-only="true" /> 
       <tx:method name="*"/>
   </tx:attributes>
</tx:advice>
< tx:advice / >默认配置如下:
  • 传播行为(propagation)是REQUIRED
  • 隔离级别(isolation level)是DEFAULT
  • 事务默认可读写(read-write)
  • 事务超时时间是数据库默认事务超时时间
  • unchecked异常RuntimeException异常触发事务回滚,checked异常Exception不会触发回滚
< tx:method / > 设置项:
属性是否必须默认值描述
nameYes与事务属性关联的方法名称。支持通配符(如, get, handle*, on*Event等
propagationNoREQUIRED事务传播行为
isolationNoDEFAULT事务隔离级别 仅当传播行为设置为 REQUIRED 或者 REQUIRES_NEW时有效.
timeoutNo-1事务超时时间(单位:秒). 仅当传播行为设置为 REQUIRED 或者 REQUIRES_NEW时有效.
read-onlyNofalse设置读写事务或者只读事务. 仅当传播行为设置为 REQUIRED 或者 REQUIRES_NEW时有效.
rollback-forNo设置多个可以触发事务回滚的异常(多个用英文逗号隔开). 如,com.foo.MyBusinessException,ServletException.
no-rollback-forNo设置多个禁止触发事务回滚的异常(多个用英文逗号隔开).如,com.foo.MyBusinessException,ServletException.

示例

tx_transaction.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/context
            http://www.springframework.org/schema/context/spring-context.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">

    <!-- 通过xml方式配置spring声明式事务 配置开始 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="studentDao" class="com.lanou3g.spring.transaction.xml.dao.StudentDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>
    
    <!-- 第一步:配置数据源 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="${jdbc.url}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="connectionProperties" value="${jdbc.characterEncoding}" />
    </bean>

    <!-- 第二步:初始化事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 第三步:配置事务AOP通知 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="insert*" rollback-for="Exception"/>
            <tx:method name="query*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 第四步:定义AOP配置(将上面的通知和表达式组装到一起) -->
    <aop:config>
        <aop:pointcut id="all_dao_method" expression="execution(* com.lanou3g.spring.transaction.xml.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="all_dao_method" />
    </aop:config>

    <!-- 通过xml方式配置spring声明式事务 配置结束 -->

</beans>

jdbc.properties数据源

jdbc.url=jdbc:mysql://localhost:3306/zzj0301
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=UTF-8

数据库对应的java类

@Getter
@Setter
public class Student {
    private int id;
    private String sname;
    private int age;
    private String gender;
    private String nickname;
}

对数据库进行操作的java类

@Setter
@Getter
public class StudentDaoImpl {

    private JdbcTemplate jdbcTemplate;

    /**
     * 插入一条student数据
     * @param map
     * @return 返回影响行数
     */
    public int insertStudent(Map map){
        Object[] args = new Object[]{
                map.get("sname"),
                map.get("age"),
                map.get("gender"),
                map.get("nickname")
        };
        int row = jdbcTemplate.update("insert into student (sname,age,gender,nickname) values (?,?,?,?)",args);

        //模拟事务异常,使程序回滚
        //int a = 2 / 0;
		
		//返回影响行数
        return row;
    }

}

程序运行入口

public class AppXML {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("tx_transaction.xml");
 		Map<String,Object> map = new HashMap<>();
        map.put("sname","王五");
        map.put("age",42);
        map.put("gender","男");
        map.put("nickname","小五");
        int row = ctx.getBean(StudentDaoImpl.class).insertStudent(map);
        System.out.println("影响了" + row + "行");
    }
    
}

注解配置方式

开启事务注解支持

XML中开启:

<!-- 开启事务注解扫描 -->
<!-- 如果定义的事务管理器名称就叫transactionManager,则此属性可以省略 -->
<tx:annotation-driven transaction-manager="txManager" />

注解方式开启:

@Configuration
@EnableTransactionManagement
public class MyTransactionConf {
}

配置事务管理器

/**
 * 通过注解的方式配置Spring声明式事务
 * 也可以将事务管理器定义到xml中,注解事务会按类型自动找到xml中配置的事务管理器
  */
@Configuration
public class MyTransactionConf {
 /**
 * 定义TransactionManager bean
 * @param dataSource
 * @return
   */
    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

在需要事物的方法上添加事务注解

// 给批量插入方法添加事务控制
@Transactional
@Override
public int[] batchInsertStudent(List<Student> studentList) {
     // 具体操作....
}
// 给查询方法添加只读事务
@Transactional(readOnly = true)
public List<Student> queryStudents() {
    // 具体操作
}

@Transactional属性说明

  • value: 指定特定的事务管理器,默认是transactionManager
  • 其他属性和xml中的的属性类似

示例

jdbc.properties数据源

jdbc.url=jdbc:mysql://localhost:3306/zzj0301
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=UTF-8

配置数据源

@PropertySource("classpath:jdbc.properties")
@Component
public class MyDataSource extends DriverManagerDataSource {
    public MyDataSource(@Value("${jdbc.driver}") String driver,@Value("${jdbc.url}") String url,@Value("${jdbc.user}") String username,@Value("${jdbc.password}") String password,@Value("${jdbc.characterEncoding}") String characterEncoding) {
        super.setDriverClassName(driver);
        super.setUrl(url);
        super.setUsername(username);
        super.setPassword(password);
        Properties properties = new Properties();
        properties.setProperty("characterEncoding",characterEncoding);
        super.setConnectionProperties(properties);
    }
}

数据库对应的java类

@Getter
@Setter
public class Student {
    private int id;
    private String sname;
    private int age;
    private String gender;
    private String nickname;
}

对数据库进行操作的java类

@Setter
@Getter
@Component
public class StudentDaoImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 插入一条student数据
     * @param map
     * @return 影响的行数
     */
    @Transactional(rollbackFor = {Exception.class})
    public int insertStudent(Map map){
        Object[] args = new Object[]{
                map.get("sname"),
                map.get("age"),
                map.get("gender"),
                map.get("nickname")
        };
        int row = jdbcTemplate.update("insert into student (sname,age,gender,nickname) values (?,?,?,?)",args);

        //模拟事务异常,使程序回滚
        //int a = 2 / 0;

        return row;
    }

}

代码运行入口

@Configuration
@ComponentScan(basePackages = "com.lanou3g.spring.transaction.annotation")
@EnableTransactionManagement    //开启事务相关注解支持
public class App {

    /**
     * 定义JdbcTemplate对象,Spring给我们封装了所有的JDBC操作
     * @param dataSource
     * @return
     *
     * 相当于xml配置中的
     * <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     *      <property name="dataSource" ref="dataSource" />
     * </bean>
     */
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    /**
     *定义事务管理器
     * @param dataSource
     * @return
     *
     * 相当于xml配置中的
     * <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     *      <property name="dataSource" ref="dataSource" />
     * </bean>
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){    //DataSourceTransactionManager是PlatformTransactionManager的一个实现类
        return new DataSourceTransactionManager(dataSource);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(App.class);
		Map<String,Object> map = new HashMap<>();
        map.put("sname","欧阳锋");
        map.put("age",42);
        map.put("gender","男");
        map.put("nickname","西毒");
        int row = ctx.getBean(StudentDaoImpl.class).insertStudent(map);
        System.out.println("影响了" + row + "行");
    }

}

回滚事务

回滚特定异常(rollback-for)
<tx:advice id="txAdvice" transaction-manager="txManager">
   <tx:attributes>
      <tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
      <tx:method name="*"/>
   </tx:attributes>
</tx:advice>
不回滚特定异常(no-rollback-for)
<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

spring事务的隔离级别和传播行为

七种传播行为

1. PROPAGATION_REQUIRED – 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

2. PROPAGATION_SUPPORTS – 支持当前事务,如果当前没有事务,就以非事务方式执行。

3. PROPAGATION_MANDATORY – 支持当前事务,如果当前没有事务,就抛出异常。

4. PROPAGATION_REQUIRES_NEW – 新建事务,如果当前存在事务,把当前事务挂起。

5. PROPAGATION_NOT_SUPPORTED – 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

6. PROPAGATION_NEVER – 以非事务方式执行,如果当前存在事务,则抛出异常。

7. PROPAGATION_NESTED – 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。

备注:常用的两个事务传播属性是1和4,即PROPAGATIONREQUIRED PROPAGATIONREQUIRES_NEW

五种隔离级别

隔离级别说明
ISOLATION_DEFAULT这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别.
ISOLATIONREADUNCOMMITTED这是事务最低的隔离级别,它允许另外一个事务可以看到这个事务未提交的数据。 这种隔离级别会产生脏读,不可重复读和幻像读。
ISOLATIONREADCOMMITTED保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。 这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。
ISOLATIONREPEATABLEREAD这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。 它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
ISOLATION_SERIALIZABLE这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。 除了防止脏读,不可重复读外,还避免了幻像读。
关键词:
幻读(虚读)

事务1读取记录时事务2增加了记录并提交,事务1再次读取时可以看到事务2新增的记录;
通俗的说,幻读就是指在一个事务内读取了别的事务插入的数据,导致前后读取不一致(insert)

不可重复读取

事务1读取记录时,事务2更新了记录并提交,事务1再次读取时可以看到事务2修改后的记录;
在一个事务内读取表中的某一行数据,多次读取结果不同.一个事务读取到了另一个事务提交后的数据.

脏读

事务1更新了记录,但没有提交,事务2读取了更新后的行,然后事务T1回滚,现在T2读取无效。
通俗的说,脏读就是指一个事务读取了一个未提交事务的数据

其他业务场景

统一异常处理、日志记录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOPSpring框架中应用AOP(面向切面编程)思想的一种方式。它使用动态代理技术来实现。默认情况下,Spring会根据被代理对象是否实现接口来选择使用JDK动态代理还是CGLIB动态代理。当被代理对象没有实现任何接口时,Spring会选择CGLIB。当被代理对象实现了接口,Spring会选择JDK官方的代理技术。但是我们也可以通过配置的方式,让Spring强制使用CGLIB动态代理。 Spring AOP的配置方式有多种,包括XML模式、XML注解模式和纯注解模式。通过这些配置方式,我们可以将AOP的切面逻辑织入到我们的业务代码中,实现各种功能,比如事务管理等。 在Spring中,事务管理是AOP的一种常见应用Spring提供了多种事务管理器的实现,例如DataSourceTransactionManager和HibernateTransactionManager等。这些事务管理器实现了Spring的事务管理器核心接口,负责提供具体的事务实现策略。我们可以根据需要选择合适的事务管理器来管理数据库操作或其他事务。同时,Spring也提供了基于注解的声明式事务配置,通过在代码中使用注解来实现事务管理。 总之,Spring AOP可以应用在各种场景中,包括但不限于事务管理。通过AOP的切面编程思想,我们可以将一些横切逻辑代码(比如事务管理、日志记录等)与业务代码解耦,提高代码的可维护性和可扩展性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring AOP 应用](https://blog.csdn.net/weixin_44152160/article/details/125360176)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值