Spring声明式异常处理理解


1、事物有四个特性:原子性、一致性、隔离性、持久性。

2、对于一次操作要么全部执行成功,要么全部不成功,这就是原子性,这也是很重要的一个特性,对于一次转账操作,对于中间的任何一个环节不成功,那么整个转账过程都是不成功的,spring对于事物管理提供了很好的支持。用一个简单的例子来说明。

所需要的jar基础包:




i、使用一个类模拟一次简单的银行转账

package com.akwolf.spring.jdbc; import org.springframework.jdbc.core.JdbcTemplate; public class SpringTransactionImpl { // Spring 对JDBC的操作也进行了封装,具体的类就是JdbcTemplate private JdbcTemplate jdbcTemplate; // 留作注入jdbcTemplate public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } /** * 模拟一下转账业务 */ public void updateBank() { String sql = "update bank b set b.balance = b.balance - 100 where b.id = 1"; jdbcTemplate.update(sql); // try { // throw new SQLException("模拟异常!") ; // } catch (SQLException e) { // e.printStackTrace(); // } sql = "update bank b set b.balance = b.balance + 100 where b.id = 2"; jdbcTemplate.update(sql); } }

ii、对于beans.xml的配置,注意要要引入相应的schema头文件


<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://192.168.17.4:3306/mytest</value> </property> <property name="username"> <value>zhangh</value> </property> <property name="password"> <value>123456</value> </property> </bean> <!-- 托管JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <!-- 托管模拟操作类SpringTransactionImpl --> <bean id="jdbcImpl" class="com.akwolf.spring.jdbc.SpringTransactionImpl"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> <!-- 声明式异常处理 --> <!-- 1、定义事物管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 2、需要进行管理的目标 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" read-only="false" /> </tx:attributes> </tx:advice> <!-- 3、使用aop切入 --> <aop:config> <aop:pointcut expression="execution(* com.akwolf.spring.jdbc.*.*(..))" id="managerMethod" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="managerMethod" /> </aop:config> </beans>



iii、测试运行

package com.akwolf.spring.jdbc; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringJdbcTest { private static ApplicationContext context ; @BeforeClass public static void init(){ context = new ClassPathXmlApplicationContext("beans.xml") ; } @Test public void testJdbc() { SpringTransactionImpl impl = (SpringTransactionImpl) context.getBean("jdbcImpl") ; impl.updateBank() ; //System.out.println(context.getBean("jdbcTemplate")); } }
这个例子很简单,自己随手记一下,注释写的还算详细,应该可以看明白,其他的就不啰嗦了!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值