zoukankan      html  css  js  c++  java
  • Spring 基于xml配置方式的事务(14)

    参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html

    我们做了相应的修改。在dao中和service中的各个类中,去掉所有注解标签。然后为为每个字段提供一个setXxx()方法

    最后就是配置applicationContext.xml文件了。内容如下:

    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
     8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    11 
    12 
    13     <!-- 读取db.properties配置信息 -->
    14     <context:property-placeholder location="classpath:db.properties"/>
    15     
    16     <!-- 配置一个C3P0数据源 -->
    17     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    18         <property name="user" value="${jdbc.user}"/>
    19         <property name="password" value="${jdbc.password}"/>
    20         <property name="driverClass" value="${jdbc.driverClass}"/>
    21         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
    22     </bean>
    23     
    24     <!-- 配置一个JdbcTemplate,用来操作数据库 -->
    25     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    26         <property name="dataSource" ref="dataSource"/>
    27     </bean>
    28     
    29     <!-- 配置dao -->
    30     <bean id="bookDao" class="com.proc.dao.BookDao">
    31         <property name="jdbcTemplate" ref="jdbcTemplate"/>
    32     </bean>
    33     <bean id="storeDao" class="com.proc.dao.StoreDao">
    34         <property name="jdbcTemplate" ref="jdbcTemplate"/>
    35     </bean>
    36     <bean id="userDao" class="com.proc.dao.UserDao">
    37         <property name="jdbcTemplate" ref="jdbcTemplate"/>
    38     </bean>
    39     
    40     <!-- 配置service -->
    41     <bean id="bookShopService" class="com.proc.service.BookShopServiceJdbcImps">
    42         <property name="bookDao" ref="bookDao"/>
    43         <property name="storeDao" ref="storeDao"/>
    44         <property name="userDao" ref="userDao"/>
    45     </bean>
    46     
    47     <!-- 配置事务管理器 -->
    48     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    49         <property name="dataSource" ref="dataSource"></property>
    50     </bean>
    51 
    52     <!-- 配置事务属性 -->
    53     <tx:advice id="advice">
    54         <tx:attributes>
    55             <tx:method name="get*" read-only="true"/>
    56             <tx:method name="find*" read-only="true"/>
    57             <tx:method name="*"/>
    58         </tx:attributes>
    59     </tx:advice>
    60     
    61     <!-- 配置事务的切入点: AOP切入 -->
    62     <aop:config>
    63         <!-- 配置切入表达式 -->
    64         <aop:pointcut expression="execution(* com.proc.service.*.*(..))" id="pointcut"/>
    65         <aop:advisor pointcut-ref="pointcut" advice-ref="advice"></aop:advisor>
    66     </aop:config>
    67 </beans>
    复制代码

      这样基于xml方式的事务就配置好了。

    代码分析:

      事务采用的是AOP的方式。所以需要配置AOP切入点。指定需要为哪些类和方法采用事务

  • 相关阅读:
    机器学习技法笔记-Lecture 4 Soft-margin support vector machine
    机器学习技法笔记-Lecture 3 Kernel support vector machine
    机器学习技法笔记-Lecture 2 Dual support vector machine
    【C#】静态构造方法与静态变量
    Fitness
    【C#】Random类中构造方法、时间种子与随机数序列的关系
    Fitness
    【量化金融阅读书籍--转载https://www.douban.com/doulist/45193230/】
    【量化金融基础知识(二)】
    【量化金融基础知识】
  • 原文地址:https://www.cnblogs.com/weiqingfeng/p/9498146.html
Copyright © 2011-2022 走看看