Spring的事务处理----基于aspectJ

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--
        把数据库的配置信息,写在一个独立的文件,编译修改数据库配置内容
        spring知道jdbc.properties文件的位置
    -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!--声明数据源DataSource,作用是连接数据库-->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <!--set注入给DruidDataSource提供连接数据库的信息-->
        <!--
            使用属性配置文件中的数据,语法 ${key}
        -->
        <property name="url" value="${jdbc.url}"/><!--setUrl()-->
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.max}"/>
     </bean>

    <!--声明的是mybatis中所提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--set注入,把数据库连接池赋给dataSource属性-->
        <property name="dataSource" ref="myDataSource"/>
        <!--mybatis主配置文件的位置
            ConfigLocation属性是Resource类型,读取配置文件
            它的赋值,使用value,指定文件的路径,使用classpath:表示文件的位置
        -->
        <property name="configLocation" value="classpath:mybatis.xml"/>
     </bean>

    <!--创建dao对象,使用SqlSessionFactory的getMapper(StudentDao.class)
        MapperScannerConfigurer:在内部调用getMapper()生成每个dao接口的代理对象
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定SqlSessionFactory对象的id-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!--指定包名,包名就是dao接口所在的包名。
            MapperScannerConfigurer会扫描这个包中的所有接口,把每个接口都执行
            一次getMapper()方法,得到每个接口的dao对象。
            创建好的dao对象放到spring容器中的。dao对象的默认名称是 接口名首字母小写
        -->
        <property name="basePackage" value="edu.tjdz.dao"/>
    </bean>

    <!--声明service-->
    <bean id="buyService" class="edu.tjdz.service.ImpI.BuyGoodsServiceImpl">
        <property name="goodsDao" ref="goodsDao"/>
        <property name="saleDao" ref="saleDao"/>
    </bean>

    <!--声明式事务处理:和源码万全分离的-->
    <!--1.声明事务管理器对象-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myDataSource"/>
    </bean>

    <!--2.声明业务方法事务属性(隔离级别,传播行为,超时时间)
         id :自定义的名称,表示<tx:advice> 和 </tx:advice>之间的配置内容个
         transaction-manager:事务管理器对象
    -->
    <tx:advice id="myadvice" transaction-manager="transactionManager">
        <!--<tx:attributes : 配置事务的属性-->
        <tx:attributes>
            <!--tx:method:给具体的方法配置属性事务,method可以有多个,分别给不同的方法设置事务属性
                    name:方法名称,1)完整的方法名称,不带有包和类。
                                  2)方法可以使用通配符,* 表示任意字符
                    propagation:传播行为,枚举值
                    isolation:隔离模式
                    rollback-for:执行的异常类名,全限定名称,只发生异常时一定回滚
            -->
            <tx:method name="buy" propagation="REQUIRED" isolation="DEFAULT" 
                       rollback-for="java.lang.NullPointerException,edu.tjdz.exce.NotEnoughException"/>
            <!--使用通配符,指的很多方法-->
            <tx:method name="add*"/>
            <!--指定修改方法-->
            <tx:method name="modify*"/>
            <!--删除方法-->
            <tx:method name="remove*"/>
            <!--查询方法,query,search,find-->
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!--配置aop-->
    <aop:config>
        <!--配置切入点表达式:指定那些包中,要使用事务
            id:切入点表达式的名称,唯一值
            expression:切入点表达式,指定哪些类要使用事务,aspectj会创建代理对象
            
            edu.tjdz.service
            edu.service
            edu.crm.service
        -->
        <aop:pointcut id="servicePt" expression="execution(* *..service..*.*(..))"/>
        
        <!--配置增强器:关联advice和pointPt
            advice-ref:通知,上面tx:advice哪里的配置
            pointcut-ref:切入点表达式的id值
        -->
        <aop:advisor advice-ref="myadvice" pointcut-ref="servicePt"/>
    </aop:config>
    
</beans>

适合大型项目,有很多类,方法,需要大量的配置事务,使用aspectJ框架功能,在spring配置文件中
    声明类,方法需要的事务,这种业务方法和事务配置万全分离。

    实现步骤:都是在xml配置文件中完成的。
    1)要使用的是aspectj框架,需要加入依赖
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.2.5.RELEASE</version>
  </dependency>

    2)声明事务管理器对象
        <bean id="xx" class="DataSourceTransactionManager"></bean>

    3)声明方法需要的事务类型(配置方法的事务属性【隔离级别,传播行为,超时】)

    4)配置aop:指定那些类需要创建代理。

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你困了吗?

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

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

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

打赏作者

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

抵扣说明:

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

余额充值