spring-aop事物

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/iwuio/article/details/84499043

       纵向重复,横向抽取。

       一个一个的方法,像是一块一块的木板。其实有的方法中有很多重复的逻辑,这样的话就可以把它抽取出来。有的同学可以能会说是不是把这些逻辑抽取出来一个简单的一个方法包含,然后再一个个的放到某个方法中是这样吗?

       其实这样算是一种,有一种情况是完全的不用管,直接在xml配置。把所有的包都配置在xml中所以无论怎么在各个class中写什么方法,会自动添加逻辑。

      在图书馆和itoo的导入中,我感觉非常的神奇,有一个步骤出错,任何一个表的任何一条记录都不会导入。就因为加了一个事务注解。真的是非常强大。

      就来说一下这个小的例子吧!

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">


<!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"  />

<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
	<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
	<property name="transactionManager" ref="transactionManager" ></property>
</bean>

<!-- 开启使用注解管理aop事物 -->
<tx:annotation-driven/>

<!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
	<property name="driverClass" value="${jdbc.driverClass}" ></property>
	<property name="user" value="${jdbc.user}" ></property>
	<property name="password" value="${jdbc.password}" ></property>
</bean>


<!-- dao -->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" >
	<property name="dataSource" ref="dataSource" ></property>
</bean>

<!-- 3.将servicespring容器 -->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" >
	<property name="ad" ref="accountDao" ></property>
	<property name="tt" ref="transactionTemplate" ></property>
</bean>
</beans>

  1.来看一下第一句话,<context:property-placeholder location="classpath:db.properties"  />这个的意思就是读取classpath:db.properties的内容。

2.下面的两个类,就是引用的特定的jar包

<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
    <property name="transactionManager" ref="transactionManager" ></property>
</bean>

3.开启注解管理aop事务,其中还有完全通过xml来进行配置的

<!-- 开启使用注解管理aop事物 -->
<tx:annotation-driven/>

说到这里还要说几个名词

连接点,切入点,                                 通知,织入,                          切面

人,   将军,                                         铠甲,人穿铠甲                    ,将军+铠甲

方法,方法+增强的方法(事务代码),事务,方法+事务的这个过程,添加好事务的方法+(事务代码)=人+两套铠甲
4.配置类和别名

<!-- dao -->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" >
    <property name="dataSource" ref="dataSource" ></property>
</bean>

<!-- 3.将servicespring容器 -->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" >
    <property name="ad" ref="accountDao" ></property>
    <property name="tt" ref="transactionTemplate" ></property>
</bean>

这样spring+spring事务简单的就配置完成了。

看一下怎么添加事务注解使用。

	private AccountDao ad;
	private TransactionTemplate tt;
	@Override
	@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
	public void transfer(Integer from, Integer to, Double money) {

		ad.decreaseMoney(from, money);
		 int t=1/0;
		//加钱
	    ad.increaseMoney(to, money);
 
	}

 调用transfer的时候,会报错两个方法都不会执行到数据库中。直接在方法中添加@Transactional就可以了。

isolation=Isolation.REPEATABLE_READ,这个代表的隔离的级别可以防止脏读和不可重复的。

propagation=Propagation.REQUIRED,传播特性,如果存在一个事务,则支持当前事务。如果没有事务则开启;

这个相对itoo的配置还是太简单了。

猜你喜欢

转载自blog.csdn.net/iwuio/article/details/84499043