spring-使用XML配置声明式事务

一、创建spring项目
    项目名称:spring101501
二、在项目中添加spring支持
    1.在项目中创建lib目录
        /lib
    2.在lib目录下添加jar包
        com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
        com.springsource.org.aopalliance-1.0.0.jar
        com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
        commons-logging.jar
        junit-4.10.jar
        log4j.jar
        mysql-connector-java-5.1.18-bin.jar
        spring-aop-3.2.0.RELEASE.jar
        spring-aspects-3.2.0.RELEASE.jar
        spring-beans-3.2.0.RELEASE.jar
        spring-context-3.2.0.RELEASE.jar
        spring-core-3.2.0.RELEASE.jar
        spring-expression-3.2.0.RELEASE.jar
        spring-jdbc-3.2.0.RELEASE.jar
        spring-tx-3.2.0.RELEASE.jar
三、在项目中添加属性文件和配置文件
    1.在项目中创建conf目录
        /conf
    2.在conf目录下添加属性文件
        属性文件名称:jdbc.properties
        属性文件内容:
        jdbc.driver = com.mysql.jdbc.Driver
        jdbc.url = jdbc:mysql://localhost:3306/spring
        jdbc.user = root
        jdbc.password =root
    3.在conf目录下添加核心配置文件
        配置文件名称:applicationContext.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/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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
            
        </beans>
四、持久层设计
    1.接口设计
        在src目录下创建接口所在的包
            包名:cn.jbit.spring101501.dao
        在包创建接口
            接口名:AccountDao.java
            接口内容:
            public interface AccountDao {
                public void outMoney(int outAccount,double money);
                public void inMoney(int inAccount,double money);
            }
    2.实现类设计
        类名:AccountDaoImpl.java
        类内容:
        public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
            @Override
            public void inMoney(int inAccount, double money) {
                String sql = "UPDATE Account SET money = money + ? WHERE id = ?";
                this.getJdbcTemplate().update(sql,money,inAccount);
            }
        
            @Override
            public void outMoney(int outAccount, double money) {
                String sql = "UPDATE Account SET money = money - ? WHERE id = ?";
                this.getJdbcTemplate().update(sql,money,outAccount);
            }
        }
五、业务层设计
    1.业务接口设计
        包名:cn.jbit.spring101501.service
        接口名:AccountService.java
        接口内容:
        public interface AccountService {
            public void transfer(int outAccount,int inAccount,double money);
        }
    2.业务实现设计
        实现类名:AccountServiceImpl.java
        实现类内容:
        public class AccountServiceImpl implements AccountService {
        
            private AccountDao accountDao;
            @Override
            public void transfer(int outAccount, int inAccount, double money) {
                accountDao.outMoney(outAccount, money);
                int a = 1/0;
                accountDao.inMoney(inAccount, money);
            }
            
            public void setAccountDao(AccountDao accountDao) {
                this.accountDao = accountDao;
            }
            public AccountDao getAccountDao() {
                return accountDao;
            }
        }
六、在核心配置文件中添加相关配置
    <!-- 1.加载属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 2.配置数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 3.配置jdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 4.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 5.配置DAO -->
    <bean id="accountDao" class="cn.jbit.spring101501.dao.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    
    <!-- 6.配置Service -->
    <bean id="accountService" class="cn.jbit.spring101501.service.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    
    <!-- 7.定义通知(Advice) -->
    <tx:advice id="accountAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 8.定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* cn.jbit.spring101501.service.*ServiceImpl.*(..))" id="accountpintcut"/>
        <aop:advisor advice-ref="accountAdvice" pointcut-ref="accountpintcut"/>
    </aop:config>
七、测试
    1.在项目中创建test目录
        /test
    2.在test目录下添加测试包
        包名:cn.jbit.spring101501.service
    3.在测试包下创建测试类
        类名:AccountServiceTest.java
        类内容:
        public class AccountServiceTest {
            @Test
            public void testTranser(){
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                AccountService accountService = (AccountService) context.getBean("accountService");
                accountService.transfer(1, 2, 200);
            }
        }

时间: 2024-08-30 15:01:09

spring-使用XML配置声明式事务的相关文章

【Spring】Spring使用XML配置声明式事务

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.事务介绍 事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用. 事务的四个关键属性(ACID) ① 原子性(atomicity):事务室一个原子操作,有一系列动作组成.事务的原子性确保动作要么全部完成,要么完全不起作用② 一致性(consistency):一旦所

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/

spring基于xml的声明式事务控制

配置文件bean.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

spring学习笔记(22)声明式事务配置,readOnly无效写无异常

在上一节内容中,我们使用了编程式方法来配置事务,这样的优点是我们对每个方法的控制性很强,比如我需要用到什么事务,在什么位置如果出现异常需要回滚等,可以进行非常细粒度的配置.但在实际开发中,我们可能并不需要这样细粒度的配置.另一方面,如果我们的项目很大,service层方法很多,单独为每个方法配置事务也是一件很繁琐的事情.而且也可能会造成大量重复代码的冗杂堆积.面对这些缺点,我们首要想到的就是我们spring中的AOP了.spring声明式事务的实现恰建立在AOP之上. 在这一篇文章中,我们介绍s

Spring学习七、声明式事务

十三.声明式事务 回顾事务 把一组事务当成一个业务来做要么都成功,要么都失败 涉及到数据一致性的问题,不能马虎 确保完整性和一致性 事务的ACID原则 原子性 一致性 隔离性 多个业务操作同一个资源,防止事务损坏 持久性 事务一旦提交,无论系统发生什么,结果都不受影响,被持久化写到存储器中 Spring中的事务管理 声明式事务:AOP 编程事务:需要在代码中 <!-- 配置声明式事务--> <bean id="transactionManager" class=&qu

spring基于注解的声明式事务控制配置

配置文件: <?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/sc

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

1.事务的介绍: 事务涉及到数据的一致性问题. 事务:要么都成功,要么都不成功! 事务的四大特性: ACID :原子性:一致性:隔离性:持久性. 编程中遇到的实际问题: 在如下的实现类(UserDaoImpl)中,执行了:先添加一个user,再删除一个user的操作,最后打印出所有的用户列表. 当我们人为的在删除代码写错时(即就不能成功执行删除操作),发现程序还是执行了添加和打印用户列表的其余两步,这就不符合事物的一致性. public class UserDaoImpl implements

Spring 基于xml配置方式的事务

参考前面的声明式事务的例子: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:/

Spring(十五)之声明式事务

声明式事务管理方法允许你在配置的帮助下而不是源代码硬编程来管理事务.这意味着你可以将事务管理从事务代码中隔离出来.你可以只使用注释或基于配置的 XML 来管理事务. bean 配置会指定事务型方法.下面是与声明式事务相关的步骤: 我们使用标签,它创建一个事务处理的建议,同时,我们定义一个匹配所有方法的切入点,我们希望这些方法是事务型的并且会引用事务型的建议. 如果在事务型配置中包含了一个方法的名称,那么创建的建议在调用方法之前就会在事务中开始进行. 目标方法会在 try / catch 块中执行