Spring整合hibernate:3、使用XML进行声明式的事务管理

配置applicationContext.xml文件


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

<?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-3.0.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springframework.org/schema/tx

           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

           http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.fz.annotation" />

    <bean

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

        <property name="locations" value="classpath:jdbc.properties" />

    </bean>

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

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

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

        <property name="packagesToScan">

            <list>

                <value>com.fz.annotation.model</value>

            </list>

        </property>

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

                <prop key="hibernate.show_sql">true</prop>

                <prop key="hibernate.format_sql">true</prop>

            </props>

        </property>

    </bean>

    <!-- 使用xml方式管理事务 -->

    <bean id="txManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

    </bean>

    <aop:config>

        <aop:pointcut

            expression="execution(public * com.fz.annotation.service..*.*(..))"

            id="bussinessService" />

        <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService"/>

    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="txManager">

        <tx:attributes>

            <!-- 所有以find开头的方法,readOnly=true -->

            <tx:method name="find*" read-only="true"/>

            <tx:method name="add*" propagation="REQUIRED"/>

            <tx:method name="insert*" propagation="REQUIRED"/>

            <tx:method name="update*" propagation="REQUIRED"/>

            <tx:method name="del*" propagation="REQUIRED"/>

            <tx:method name="delete*" propagation="REQUIRED"/>

        </tx:attributes>

    </tx:advice>

</beans>

1.定义一个aop:pointcut的切入点,也就是需要处理事务的具体service

2.定义一个具体该怎么做的指导aop:advisor,需要指导的pointcut就是刚才定义的pointcut。指导的建议可以自定义一个建议txAdvice

3.txAvice需要使用到数据库,所以需要一个Spring具体管理的事务对象txManager。

4.Spring管理事务又需要SessionFactory,所以又配置一个SessionFactory的属性。而SessionFactory里又指定了一个数据源。

来自为知笔记(Wiz)

时间: 2024-07-31 21:31:35

Spring整合hibernate:3、使用XML进行声明式的事务管理的相关文章

9.Spring整合Hibernate_2_声明式的事务管理(Xml的方式)

使用xml的方式进行声明式的事务管理 推荐使用xml的方式,因为可以同时为多个方法进行声明 1 <!-- 开启Spring中的事务管理(声明式的事务管理) xml--> 2 3 <!-- 不管是通过 xml 还是注解的方式 来进行声明式的事务管理,都需要 加载TransactionManager或 DataSouce 4 因为 事务是与是与数据库相关的,需要数据库一些配置信息 5 --> 6 <bean id="txManager" class="

8.Spring整合Hibernate_2_声明式的事务管理(Annotation的方式)

声明式的事务管理(AOP的主要用途之一) (Annotation的方式) 1.加入annotation.xsd 2.加入txManager bean 3.<tx:annotation-driven 1 <tx:annotation-driven transaction-manager="txManager"/> 2 <bean id="txManager" class="org.springframework.orm.hiberna

Spring基于声明式的事务管理

事务管理 Spring提供了编码式和声明式事务管理的支持. 编码式事务允许用户在代码中精确定义事务的边界,而声明式事务(基于AOP)用助于用户将操作与事务解耦. 选择编码式还是声明式在很大程度上是在细粒度控制和易用性之间权衡. Spring并不直接管理事务,而是提供多种事务管理器,将它们事务管理职责委托给JTA或其他持久化机制所提供的平台相关的事务实现.每个事务管理器都会充当某一特定平台的事务实现的门面,这使得用户在Spring中使用事务时,不用关注事务的实际实现是什么. JDBC事务 <bea

spring整合hibernate的applicationContext.xml文件配置以及web.xml

applicationContext.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.spri

Rabbitmq与spring整合之重要组件介绍——AMQP声明式配置&amp;RabbitTemplate组件

上一节是使用rabbitAdmin的管理组件进行声明队列,交换器,绑定等操作,本节则是采用AMQP声明式配置来声明这些东西.AMQP声明主要是通过@Bean注解进行的. 配置: 1 package com.zxy.demo.config; 2 3 import org.springframework.amqp.core.Binding; 4 import org.springframework.amqp.core.BindingBuilder; 5 import org.springframew

Spring 整合hibernate和mybatis的 applicationContext.xml的配置

Spring整合hibernate的applicationContext.xml配置 1.注解方式的实现 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x

Spring整合Hibernate的步骤

为什么要整合Hibernate?1.使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean2.使用Spring管理Session对象  HibernateTemplate3.使用Spring的功能实现声明式的事务管理 整合Hibernate的步骤:1.配置SessionFactory(能够自己主动完毕) <bean id="sessionFactory"  class="org.springframework.o

【Spring】Spring使用XML配置声明式事务

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.事务介绍 事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用. 事务的四个关键属性(ACID) ① 原子性(atomicity):事务室一个原子操作,有一系列动作组成.事务的原子性确保动作要么全部完成,要么完全不起作用② 一致性(consistency):一旦所

尚硅谷Spring整合Hibernate基于xml配置

描述:这是一个最简单网上书城demo. 下载地址:http://download.csdn.net/detail/u013488580/8370899 1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory 2). 让 Hibernate 使用上 Spring 的声明式事务 2. 整合步骤: 1). 加入 hibernate ①. jar 包 ②. 添加 hibernate 的配置文件: hibernate