spring 注解管理事务 (注解事务管理)
参考网址:
http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html
配置文件(applicationContext.xml)内容:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-autowire="default" default-lazy-init="false">
<context:component-scan base-package="demo"></context:component-scan>
<!-- 导入配置信息 -->
<context:property-placeholder location="classpath:demo.properties" />
<!-- 导入其它配置文件 -->
<import resource="applicationContext-service.xml"/>
<import resource="applicationContext-redis.xml"/>
<import resource="applicationContext-cache.xml"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
lazy-init="false">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>demo.model.db</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="baseHibernateDAO" class="demo.datacenter.BaseHibernateDAO"
abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 注解式事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
在需要使用事务的类或接口上(一般是服务层)写入 @Transactional,表示该类的所有 public 方法都有了事务管理;
如果单独在方法前面加上 事务注解, 则会覆盖类前面的注解;