【Spring AOP】学习记录(二)

来源:互联网 发布:三角形测试用例java 编辑:程序博客网 时间:2024/05/21 12:40

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

学啥

今天学习了一下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
原创粉丝点击