Spring整合hibernate:三、使用XML進行聲明式的事務管理

配置applicationContext.xml文件spring

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" ?>
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     < 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的切入點,也就是須要處理事務的具體servicesql

2.定義一個具體該怎麼作的指導aop:advisor,須要指導的pointcut就是剛纔定義的pointcut。指導的建議能夠自定義一個建議txAdvice數據庫

3.txAvice須要使用到數據庫,因此須要一個Spring具體管理的事務對象txManager。express

4.Spring管理事務又須要SessionFactory,因此又配置一個SessionFactory的屬性。而SessionFactory裏又指定了一個數據源。apache




相關文章
相關標籤/搜索