【Spring AOP】学习记录(二)

国庆过得好像有点忙,是不是生活过得简单了,就有时间享福了?当然国庆假期也抽点时间来学习学习,不然赶不上年轻人的脚步

学啥

今天学习了一下aop的事务管理,没分析内部,估计只不过对使用进行了封装,让我们使用更加简便而已

功能

  • 可以通过切面编程对指定的进行事务管理
  • 可以控制异常的类型才进行回滚
  • 可以控制事务的timeout

Propagation取值:

备注
REQUIRED(默认值)在有transaction状态下执行;如当前没有transaction,则创建新的transaction
SUPPORTS如当前有transaction,则在transaction状态下执行;如果当前没有transaction,在无transaction状态下执行
MANDATORY必须在有transaction状态下执行,如果当前没有transaction,则抛出异常IllegalTransactionStateException
REQUIRES_NEW创建新的transaction并执行;如果当前已有transaction,则将当前transaction挂起
NOT_SUPPORTED在无transaction状态下执行;如果当前已有transaction,则将当前transaction挂起
NEVER在无transaction状态下执行;如果当前已有transaction,则抛出异常IllegalTransactionStateException

例子1(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: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">

    <aop:config>
        <!--定义切入点1,要对指定的进行管理事物-->
        <aop:pointcut id="defaultServiceOperation"
                expression="execution(* x.y.service.*Service.*(..))"/>

        <!--定义切入点2,要对指定的进行管理事物-->
        <aop:pointcut id="noTxServiceOperation"
                expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/>

        <!--对上面的切入点1采用defaultTxAdvice实例进行管理-->
        <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/>

        <!--对上面的切入点2采用noTxAdvice实例进行管理-->
        <aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/>

    </aop:config>

    <!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) -->
    <bean id="fooService" class="x.y.service.DefaultFooService"/>

    <!-- this bean will also be transactional, but with totally different transactional settings -->
    <bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/>

    <!--对方法名get开始的,只允许读,不允许修改等-->
    <tx:advice id="defaultTxAdvice">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--在无transaction状态下执行;如果当前已有transaction,则抛出异常IllegalTransactionStateException。-->
    <tx:advice id="noTxAdvice">
        <tx:attributes>
            <tx:method name="*" propagation="NEVER"/>
        </tx:attributes>
    </tx:advice>

    <!-- other transaction infrastructure beans such as a PlatformTransactionManager omitted... -->

</beans>

分析说明

其实还是利用了AOP那一套,有稍微一点点不一样

  • tx:advice 事务控制点
  • transaction-manager 事务管理者

例子2(注解)

直接看例子再分析分析

<?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">



    <!--允许注解管理事务-->
    <tx:annotation-driven transaction-manager="txManager" order="200"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
        <property name="username" value="scott"/>
        <property name="password" value="tiger"/>
    </bean>

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

</beans>

java代码

@Transactional(readOnly = true)
public class DefaultFooService implements FooService {

    public Foo getFoo(String fooName) {
        // do something
    }

    // these settings have precedence for this method
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void updateFoo(Foo foo) {
        // do something
    }
}

重点

  • <tx:annotation-driven transaction-manager="txManager" order="200"/> 支持事务注解
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值