Spring入门Blog[十二、Spring中Annotation声明事务]

Spring的声明式事务Annotation的实现------AOP应用

首先先讨论一个问题:

事务放在service层还是放在dao层呢?

Dao层只是针对于某一个实体进行CRUD的操作。如果在增加删除一个表的时候。比如USER我们要在LOG表中记录相应的日志。那么在DAO层如何完成。所以说Service层可能操作多个实体类。所以事务加载service层比较合适。因为不论我们保存哪一个实例出现了错误我们都可以回滚。而不是Log插入了,User没插入。反之亦然,这都是我们不愿意看到的。

 

下面举例Spring中事务通过Annotation方式是如何实现的:

1、  加入tx开头的命名空间并且配置事务manager和driven。代码如下:

<?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: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/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<!-- 配置容器资源扫描的包 -->
	<context:component-scan base-package="com.spring" />
	<!-- 将前面类写入容器 -->
	<bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />



	<!--
		配置数据源 <bean id="myDataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close"> <property name="driverClassName"
		value="com.mysql.jdbc.Driver"/> <property name="url"
		value="jdbc:mysql://localhost:3306/sms"/> <property name="username"
		value="root"/> <property name="password" value="root"/> </bean>
	-->

	
	<!-- placeholder 占位符 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	<!-- 配置dataSource -->
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 将配置好的dataSource注入到SessionFactory中-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
      <list>
        <value>com/spring/model/user.hbm.xml</value>
        <value>com/spring/model/userlog.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
        hibernate.show_sql=true
        hibernate.hbm2ddl.auto=create
      </value>
    </property>
  </bean>
	
	
	<!-- 声明式事务管理,事务需要数据源,从sessionFactory中拿到
	这是一个AOP的应用 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 声明式事务注解的配置 -->
<tx:annotation-driven />
</beans>
2、在Java文件上使用注解标签:
//在Service层加入事务的控制。具体逻辑不在给予。
//在下面方法前面加逻辑
	/*事务的传播性。如果当前执行环境中有事务,那么则会一直在
	 * 传播环境中传播下去。如果没有事务那么则会创建一个事务
	*/
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(User u){
		u.setName("zhanglong");
		UserLog log = new UserLog();
		log.setMsg("user added");
		userDaoImpl.save(u);
		userLogDaoImpl.save(log);
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值