spring事务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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--引入外部配置文件-->
    <context:property-placeholder location="classpath:jdbcConfig.properties"></context:property-placeholder>
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--如果用配置文件的方式, 就用下面这种方式,将数据源注入进去, 并且让AccountDaoImpl继承jdbcDaoSupport-->
    <!--如果是用注解的方式, 直接使用 Autowired 注入jdbcTemplate即可 -->
    <!--配置AccountDaoImpl 持久层-->
    <bean class="cn.zhou.dao.impl.AccountDaoImpl" id="accountDao">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置AccountService 业务层-->

    <bean class="cn.zhou.service.impl.AccountServiceImpl" id="accountService">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!--spring中基于xml的声明式事务控制配置步骤
        1. 配置事务管理器
        2. 配置事务的通知
                此时 我们需要导入 事务的约束  ===> tx的名称空间和约束,同时也需要aop的
                使用tx:advice 标签配置事务通知
                    属性:
                        id: 给事务通知起一个唯一标志
                        transaction-manager: 给事务通知提供一个事务管理器引用
        3. 配置aop中通用切入点表达式
        4. 建立事务通知和切入点表达式对应关系
        5. 配置事务的属性
                是在事务的通知txAdvice标签的内部

    -->
    <!--1. 配置事务管理器-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <!--注入DataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--2. 配置事务的通知-->
    <tx:advice transaction-manager="transactionManager" id="txAdvice">
        <!--5. 配置事务的属性
                    isolation=""            用于指定事务的隔离级别, 默认值是DEFAULT ,表示使用数据库的默认隔离级别
                    propagation=""          用于指定事务的传播行为, 默认值是REQUIRED,表示一定会有事务, 增删改的选择, 查询方法可以选择SUPPORTS
                    read-only=""            用于指定事务是否只读, 只有查询方法才能设置为true. 默认值是false,表示读写
                    timeout=""              用于指定事务的超时时间,默认值是 -1, 表示用不超时. 如果指定了数值, 以秒为单位
                    rollback-for=""         用于指定一个异常, 当产生该异常时, 事务回滚, 当产生其他异常时,事务不回滚. 没有默认值. 表示任何异常都回滚
                    no-rollback-for=""      用于指定一个异常, 当产生该异常时,事务不回滚, 产生其他异常时,事务回滚. 没有默认值. 表示任何异常都回滚
        -->
        <!--find*的优先级肯定是高于*的优先级,这里就要求 在service的实现类中命名的时候 需要规范,查询操作的时候 需要以find开头
			才能匹配到查询相关的事务控制
		-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>


    <!-- 配置aop-->
    <aop:config >
        <!--3. 配置切入点表达式-->
        <aop:pointcut id="pt1" expression="execution(* cn.zhou.service.impl.*.*(..))"></aop:pointcut>
        <!--4. 建立切入点表达式和事务通知对应的关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>

</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值