现在的位置: 首页 > 综合 > 正文

spring2.5好用的@Transactional

2012年01月09日 ⁄ 综合 ⁄ 共 3130字 ⁄ 字号 评论关闭

@Transactional对事务管理。

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

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

@Transactional

public
class HotelCompanySMSServiceImpl implements IHotelCompanySMSService {

//
类的主体

}

 

 

 

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

 

//如果发生Caused by:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching
wildcard is strict, but no declaration can be found for element
'context:component-scan'.异常请查看xsi:schemaLocation中是否有红色的配置。

 

<?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>

 

 

抱歉!评论已关闭.