spring 注解管理事务

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 方法都有了事务管理;
如果单独在方法前面加上 事务注解, 则会覆盖类前面的注解;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值