spring-声明式事务管理

简介:
一、创建spring项目
    项目名称:spring101311
二、在项目上添加jar包
    1.在项目中创建lib目录
        /lib
    2.在lib目录下添加spring支持
        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目录
    2.在conf目录下添加属性文件
        属性文件名称:jdbc.properties
        属性文件内容:
        jdbc.url=jdbc:mysql://localhost:3306/spring
        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.username=root
        jdbc.password=root
    2.在conf目录下添加spring核心配置文件
        配置文件名称: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"
                       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.xsd">
                
                <!-- 1.加载属性文件 -->
                <context:property-placeholder location="classpath:jdbc.properties"/>
                
                <!-- 2.配置数据库连接池 -->
                <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                    <property name="jdbcUrl" value="${jdbc.url}"></property>
                    <property name="driverClass" value="${jdbc.driver}"></property>
                    <property name="user" value="${jdbc.username}"></property>
                    <property name="password" value="${jdbc.password}"></property>
                </bean>
        </beans>
四、实现bean设计
    1.在src目录下创建实体bean的包
        包名:cn.jbit.spring101310.domain
    2.在包下创建实体bean
        public class Account {
            private Integer id;
            private String name;
            private Double money;
            //省略get and set
        }
五、设计Dao层
    1.在src目录下创建dao层的包
        包名:cn.jbit.spring101310.dao
    2.在包下创建dao层的接口与实现类
        1)接口设计
            接口名称:IAccountDao.java
            接口内容:
            public interface IAccountDao {
                /**
                 * 转出
                 */
                public void outMoney(Account outaccount);
                /**
                 * 转入
                 */
                public void inMoney(Account inaccount);
            }
        2)接口实现类设计
            实现类名称:AccountDaoImpl.java
            实现类内容:
            public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
                /**
                 * 转入
                 */
                @Override
                public void inMoney(Account inaccount) {
                    String sql = "update account set money = money + ? where id = ?";
                    this.getJdbcTemplate().update(sql,inaccount.getMoney(),inaccount.getId());
                }
            
                /**
                 * 转出
                 */
                @Override
                public void outMoney(Account outaccount) {
                    String sql = "update account set money = money - ? where id = ?";
                    this.getJdbcTemplate().update(sql,outaccount.getMoney(),outaccount.getId());        
                }
            }
六、在核心配置文件中配置Dao
    <!-- 3.配置Dao -->
    <bean id="accountDao" class="cn.jbit.spring101310.dao.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

七、在核心配置文件中配置事务相关信息
    <!-- 4.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
八、业务层设计
    1.在src目录下创建业务层的包
        包名:cn.jbit.spring101310.service
    2.在包下创建业务层的接口与实现类
        1)接口设计
            接口名称:AccountService.java
            接口内容:
            public interface AccountService {
                public void transfer(Account outAccount,Account inAccount);
            }
        2)接口实现类设计
            实现类名称:AccountServiceImpl.java
            实现类内容:
            public class AccountServiceImpl implements AccountService {
            
                private IAccountDao accountDao;
                @Override
                public void transfer(final Account outAccount, final Account inAccount) {
                    //转出
                    accountDao.outMoney(outAccount);
                    int a = 1/0;
                    //转入
                    accountDao.inMoney(inAccount);
                }
                //省略get and set
            }
九、在核心配置文件中配置业务层
    <!-- 5.配置Service -->
    <bean id="accountService" class="cn.jbit.spring101310.service.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
十、在核心配置文件中配置代理
    <!-- 6.创建代理 -->
    <bean id="accountServiceproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!--
            引用事务管理器 
         -->
        <property name="transactionManager" ref="transactionManager"></property>
        <!--
            为哪个类创建代理 
         -->
        <property name="target" ref="accountService"></property>
        <property name="transactionAttributes">
            <props>
                <!--
                    *:所有方法
                    PROPAGATION_REQUIRED:默认的事务传播行为
                 -->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
十一、测试
    1.在项目上创建test目录
        /test
    2.在test目录下创建测试包
        包名:cn.jbit.spring101310.service
    3.在测试包下创建测试类
        测试类名:AccountServiceTest.java
        测试类的内容:
        public class AccountServiceTest {
            @Test
            public void testTranser(){
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                AccountService accountService = (AccountService) context.getBean("accountService");
                Account outAccount = new Account();
                outAccount.setId(1);
                outAccount.setMoney(500D);
                Account inAccount = new Account();
                inAccount.setId(2);
                inAccount.setMoney(500D);
                accountService.transfer(outAccount, inAccount);
            }

        }


本文转自  素颜猪  51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1563387

相关文章
|
5月前
|
监控 Java 数据库连接
《深入理解Spring》事务管理——数据一致性的守护者
Spring事务管理确保数据一致性,支持声明式与编程式两种方式。通过@Transactional注解简化配置,提供传播行为、隔离级别、回滚规则等灵活控制,结合ACID特性保障业务逻辑可靠执行。
|
11月前
|
Java 关系型数据库 MySQL
深入解析 @Transactional——Spring 事务管理的核心
本文深入解析了 Spring Boot 中 `@Transactional` 的工作机制、常见陷阱及最佳实践。作为事务管理的核心注解,`@Transactional` 确保数据库操作的原子性,避免数据不一致问题。文章通过示例讲解了其基本用法、默认回滚规则(仅未捕获的运行时异常触发回滚)、因 `try-catch` 或方法访问修饰符不当导致失效的情况,以及数据库引擎对事务的支持要求。最后总结了使用 `@Transactional` 的五大最佳实践,帮助开发者规避常见问题,提升项目稳定性与可靠性。
1631 12
|
XML Java 数据库连接
Spring高手之路25——深入解析事务管理的切面本质
本篇文章将带你深入解析Spring事务管理的切面本质,通过AOP手动实现 @Transactional 基本功能,并探讨PlatformTransactionManager的设计和事务拦截器TransactionInterceptor的工作原理,结合时序图详细展示事务管理流程,最后引导分析 @Transactional 的代理机制源码,帮助你全面掌握Spring事务管理。
285 2
Spring高手之路25——深入解析事务管理的切面本质
|
Java 数据库连接 数据库
spring复习05,spring整合mybatis,声明式事务
这篇文章详细介绍了如何在Spring框架中整合MyBatis以及如何配置声明式事务。主要内容包括:在Maven项目中添加依赖、创建实体类和Mapper接口、配置MyBatis核心配置文件和映射文件、配置数据源、创建sqlSessionFactory和sqlSessionTemplate、实现Mapper接口、配置声明式事务以及测试使用。此外,还解释了声明式事务的传播行为、隔离级别、只读提示和事务超时期间等概念。
spring复习05,spring整合mybatis,声明式事务
|
Java Spring 容器
Spring IOC、AOP与事务管理底层原理及源码解析
【10月更文挑战第1天】Spring框架以其强大的控制反转(IOC)和面向切面编程(AOP)功能,成为Java企业级开发中的首选框架。本文将深入探讨Spring IOC和AOP的底层原理,并通过源码解析来揭示其实现机制。同时,我们还将探讨Spring事务管理的核心原理,并给出相应的源码示例。
464 9
|
Java 数据库连接 数据库
Spring基础3——AOP,事务管理
AOP简介、入门案例、工作流程、切入点表达式、环绕通知、通知获取参数或返回值或异常、事务管理
Spring基础3——AOP,事务管理
|
XML Java 数据库
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
这篇文章是Spring5框架的实战教程,详细介绍了事务的概念、ACID特性、事务操作的场景,并通过实际的银行转账示例,演示了Spring框架中声明式事务管理的实现,包括使用注解和XML配置两种方式,以及如何配置事务参数来控制事务的行为。
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
|
Java 数据库连接 API
Spring事务管理嵌套事务详解 : 同一个类中,一个方法调用另外一个有事务的方法
Spring事务管理嵌套事务详解 : 同一个类中,一个方法调用另外一个有事务的方法
1689 1
|
存储 安全 Java
实现基于Spring Cloud的分布式配置管理
实现基于Spring Cloud的分布式配置管理
|
Java Spring 开发者
掌握Spring事务管理,打造无缝数据交互——实用技巧大公开!
【8月更文挑战第31天】在企业应用开发中,确保数据一致性和完整性至关重要。Spring框架提供了强大的事务管理机制,包括`@Transactional`注解和编程式事务管理,简化了事务处理。本文深入探讨Spring事务管理的基础知识与高级技巧,涵盖隔离级别、传播行为、超时时间等设置,并介绍如何使用`TransactionTemplate`和`PlatformTransactionManager`进行编程式事务管理。通过合理设计事务范围和选择合适的隔离级别,可以显著提高应用的稳定性和性能。掌握这些技巧,有助于开发者更好地应对复杂业务需求,提升应用质量和可靠性。
239 0