spring框架(四)——Spring中的事务控制

注意:spring的jdbc模块笔者这里不做详解, 因为实际中用的不多,(不过还真有用的,笔者这里什么时候用什么时候在整理),那么这个模块的事务,要做一下整理。

编程式事务、这里不讲,就是将事务的开启关闭写在代码里。不做重点。

1 spring的声明式事务控制(重点)

编程式事务管理将数据层提交事务的代码加入到逻辑层,与Spring无侵入式编程的主思想有冲突,实际开发过程中,往往采用声明式事务管理形式
通过编程式事务管理的代码不难看出,在业务逻辑层对应的业务上添加某些代码即可完成整体事务管理的操作,使用SpringAOP的思想,将公共的代码加入后,即可完成对应的工作,这就是声明式事务管理的核心机制。

1.1 基于XML的声明式事务配置

a、导入spring事务支持的jar包spring-tx-3.2.0.RELEASE.jar

b、引入spring事务的名称空间


c、配置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/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.xsd 
      					   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">
	<!-- 把Dao交给Spring管理 -->
    <bean id="accountDao" class="springTX.dao.impl.AccountDaoImpl">
    	<!-- 为dao的父类JdbcDaoSupport注入一个数据源 -->
    	<property name="dataSource" ref="driverManagerDataSource"></property>
    </bean>
    
    <!-- 把业务层也交给Spring -->
    <bean id="accountService" class="springTX.service.impl.AccountServiceImpl">
    	<property name="accountDao" ref="accountDao"></property>
    </bean>

	<!-- 事务控制:声明式事务,重点。基于XML的。
		 前期准备:
		 	1.导入aop的jar包
		 	2.确保spring-tx-xxxx.jar在你的工作空间中
		 步骤:
		 	1.配置一个事务管理器,并为其注入数据源
		 	2.配置事务的通知.id是为其指定一个唯一标识。transaction-manager指定事务通知使用的管理器
		 	3.配置AOP,指定切面使用的通知。<aop:advisor advice-ref="" pointcut="切入点表达式"/>它就是指定已经配置好的通知类型。
	-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	<tx:advice id="txAdivce" transaction-manager="transactionManager">
		<!-- 配置事务的相关属性 -->
		<tx:attributes>
			<!-- tx:method 是用于指定方法名称和事务属性的关系。
				 name:指定方法的名称。可以使用通配符*,也可以部分通配
				 isolation:事务的隔离级别。默认值是:DEFAULT。当是default时是看数据库的隔离级别。 
				 rollback-for:该属性用于配置一个异常,当产生该异常时回滚。
				 no-rollback-for:该属性用于配置一个异常,当产生该异常时不回滚。
				 propagation:指定事务的传播行为。 默认值是:REQUIRED
				 read-only:指定当前事务是否是只读事务。默认值是false,不是只读的。
				 timeout:指定超时时间。默认值是-1。以秒为单位
			 -->
			<tx:method name="*" />
			<tx:method name="find*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:advisor advice-ref="txAdivce" pointcut="execution(* springTX..*.*(..))"/>
	</aop:config>
	<!-- 配置Spring的内置数据源 -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 给数据源注入参数 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/spring_tx"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
</beans>
解释如下图:

OK、基于xml的声明式事务、配置完毕。

2 基于注解的声明式事务配置

a、把spring容器管理改为注解配置的方式(开启要扫描的包)

b、开启注解事务的支持


配置结果如下:

<?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"
       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/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/context 
      					   http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 事务控制:声明式事务,重点。基于注解的。
		 前期准备:
		 	1.导入context的名称空间
		 	2.确保spring-tx-xxxx.jar在你的工作空间中
		 步骤:
		 	1.设置Spring要扫描的包
		 	2.开始Spring对注解式事务的支持。并且指定事务管理器类
		 	3.把Dao和service都交给Spring管理
		 	4.由于我们使用了注解,所以不能用JdbcDaoSupport,这个时候我们需要定义JdbcTemplate,为其注入数据源
	-->
    <context:component-scan base-package="springTX"></context:component-scan>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	
	<!-- 配置Spring的内置数据源 -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 给数据源注入参数 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/srping_tx"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
</beans>
c、在需要事务支持的地方加入@Transactional注解(一般在service层控制事务)
d、@Transactional注解配置的位置

  • 用在业务实现类上:该类所有的方法都在事务的控制范围

  • 用在业务接口上:该接口的所有实现类都起作用

  • 通过注解指定事务的定义信息

OK、基于注解的事务也整理完毕!

注意:对于事务上还有什么不了解的地方,可以观看笔者整理的spring==注解事务问题以及xml事务的配置、

url:http://blog.csdn.net/sinat_38259539/article/details/77678557

猜你喜欢

转载自blog.csdn.net/sinat_38259539/article/details/78392933