spring2.5好用的@Transactional

@Transactional对事务管理。

方法操作中需要先执行一个方法再执行另一个方法,如果不对事务进行管理,如果第二个方法执 行失败则第一个方法不会回滚,会造成很多的错误数据。

在此给该方法加上事务管理。简单配置即可。

@Transactional

public class HotelCompanySMSServiceImpl implements IHotelCompanySMSService {

//类的主体

}

将applicationcontext.xml 配置好后只要在类的头上加上 @Transactiona即可。

 

 

<?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:p="http://www.springframework.org/schema/p"  xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

< bean  id="db-properties"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"

p:ignoreUnresolvablePlaceholders="true">

< property  name="locations">

< list>

< value>classpath:DataBase.properties

< /value>

< /list>

< /property>

< /bean>

<!-- 支持标注 -->

<context:annotation-config/>

<!-- 支持 @Transactional 标记 -->

<tx:annotation-driven proxy-target-class="true"  transaction-manager="transactionManager"/>

< !-- 配置同一个数据源 --> 

<bean id="daoTemplate" abstract="true"  lazy-init="true"> 

   <property name="dataSource"  ref="dataSource" /> 

</bean> 

< bean  id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

< property  name="driverClassName">

< value>${db.driver}</value>

< /property>

< property  name="url">

< value>${db.url}</value>

< /property>

< property  name="username">

< value>${db.username}</value>

< /property>

< property  name="password">

< value>${db.password}</value>

< /property>

< property  name="initialSize">

< value>${db.pool.initial}</value>

< /property>

< property  name="maxActive">

< value>${db.pool.max}</value>

< /property>

< property  name="defaultAutoCommit" value="false" />

< /bean>

< bean  id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource" />

< bean id="userDao"  class="com.sn.hotel.dao.impl.HotelUserDaoImpl">

  < property  name="dataSource" ref="dataSource"/>

   < /bean>

< bean id="transactionTemplate"

class="org.springframework.transaction.support.TransactionTemplate">

< property  name="transactionManager">

< ref bean="transactionManager"  />

< /property>

< /bean>

<!-- 多个包名用逗号隔开, 但不能有空格

配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

-->

    <context:component-scan  base-package="com.sn.hotel.ws.service.impl"/> 

</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值