spring的学习____12 声明式事务(AoP的横向织入)

1.事务的介绍:

事务涉及到数据的一致性问题。

事务:要么都成功,要么都不成功!

事务的四大特性: ACID :原子性;一致性;隔离性;持久性。

编程中遇到的实际问题:

在如下的实现类(UserDaoImpl)中,执行了:先添加一个user,再删除一个user的操作,最后打印出所有的用户列表。

当我们人为的在删除代码写错时(即就不能成功执行删除操作),发现程序还是执行了添加和打印用户列表的其余两步,这就不符合事物的一致性。

public class UserDaoImpl implements UserDao {

    //bean中注册的SqlSession对象,此处可以拿来直接用
  private SqlSession sqlSession;
    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    //实现接口中的方法
    public List<User> getUserList() {

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        User u=new User();
        u.setId(7);
        u.setName("老六");
        u.setPwd("6666666");

       //添加一个用户
       mapper.add(u);

       //删除一个用户 
       mapper.delete(6);

        //返回用户列表信息
        return mapper.getUserList();
    }
    
    //感觉就和Service层,数据库的操作在xml文件中已经定义好
    //此处接口方法可以为空,或者直接调用就行
    public int add(User user) {
        return 0;
    }

    public int delete(int id) {
        return 0;
    }


}

2.此处引入声明式事务解决此问题:

2.1Spring中支持两种事务的处理机制:

编程式事务:把所有的事务代码都写在业务中;

声明式事务:使用AoP横切进去(推荐使用);

要开启Spring的事务处理能力,在Spring的配置文件中创建一个DataSourceTransactionManager对象:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <constructor-arg ref="dataSource" />
</bean>

2.2事务的传播级别:

key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:

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

配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/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">


    <!--声明式事务开始-->
    <!--需要配置一个事务管理器,参数需要一个数据源-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource" />
    </bean>

    <!--配置声明事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--
            name:哪些方法要使用事务【方法】
            propagation:配置事务的传播特性
            REQUIRED:如果没有事务,就新建一个事务。
            -->
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>

        </tx:attributes>
    </tx:advice>

    <!--配置aop织入事务,注意点:需要导入织入的包:aspectj-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="txPointCut" expression="execution(* com.kuang.dao.UserDaoImpl.*(..))"/>
        <!--通知-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

    <!--声明式事务结束-->




    <!--1.配置数据源,我们使用的是spring的数据源,还可以使用第三方的数据源
        dbcp,c3p0
        com.mchange.v2.c3p0.ComboPooledDataSource
        org.apache.commons.dbcp.BasicDataSource
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--2.配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--关联mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--3.创建selSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!--4.接口实现类注入sqlSession-->
    <bean id="userDaoImpl" class="com.kuang.dao.UserDaoImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>



</beans>

 

猜你喜欢

转载自www.cnblogs.com/xbfchder/p/11273614.html