加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring之基于xml文件来配置事务(五)

发布时间:2020-12-15 01:13:06 所属栏目:大数据 来源:网络整理
导读:接上一节。 本节实现如何通过基于xml文件的方式,而不是基于注解的方式来配置事务。 1.将相应文件中的注解全部去掉 2.为这三个属性加上getters和setters方法。 private JdbcTemplate jdbcTemplate; BookShopDao bookShopDao; private BookShopService bookSh

接上一节。

本节实现如何通过基于xml文件的方式,而不是基于注解的方式来配置事务。

1.将相应文件中的注解全部去掉

2.为这三个属性加上getters和setters方法。

private JdbcTemplate jdbcTemplate;
 BookShopDao bookShopDao;
private BookShopService bookShopService;

3.将目录调整为如下结构:

4.在applicationContext-tx.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: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 http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    <!-- 导入资源文件 -->
    context:property-placeholder location="classpath:db.properties"/>
    
     配置 C3P0 数据源 bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        property name="user" value="${jdbc.user}"></property="password"="${jdbc.password}"="jdbcUrl"="${jdbc.jdbcUrl}"="driverClass"="${jdbc.driverClass}">

        ="initialPoolSize"="${jdbc.initPoolSize}"="maxPoolSize"="${jdbc.maxPoolSize}">
    </bean 配置 Spirng 的 JdbcTemplate ="jdbcTemplate" 
        class="org.springframework.jdbc.core.JdbcTemplate" ref="dataSource"="bookShopDao" class="com.gong.spring.tx.xml.BookShopDaoImpl"="jdbcTemplate"="bookShopService"="com.gong.spring.tx.xml.service.impl.BookShopServiceImpl"="bookShopDao"="cashier"="com.gong.spring.tx.xml.service.impl.CashierImpl"="bookShopService" 1.配置事务管理器 ="transactionManager"="org.springframework.jdbc.datasource.DataSourceTransactionManager" 2.配置事务属性 tx:advice ="txAdice" transaction-manager="transactionManager"tx:attributes>
            tx:method ="purchase" propagation="REQUIRES_NEW"/>
            ="*"/>
        tx:advice 3.配置事务切入点 ,并将切入点和事务属性关联起来 aop:configaop:pointcut expression="execution(* com.gong.spring.tx.xml.service.*.*(..))" 
        id="txPointCut"aop:advisor advice-ref pointcut-ref/>
    >
beans>

基于xml文件配置的方式配置事务有三个步骤:

(1)配置事务管理器

(2)配置事务属性

?(3)配置事务切入点,将切入点和事务属性关联起来。

expression="execution(* com.gong.spring.tx.xml.service.*.*(..))是将service下的所有实现类的所有方法都加入到切入点。然后切入点和事务属性关联起来,

最后的结果与基于注解的配置方式是一致的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读