评:
Spring配置文件中关于事务配置总是由三个组成部分,DataSource、TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块!
首先我创建了两个类,一个接口一个实现:
1. package
2. public interface
3. public void
4. }
实现:
1. package
2. import
3. import
4. public class UserDaoImpl extends HibernateDaoSupport implements
5. public void
6. }
7. }
第一种:每个Bean都有一个代理:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:aop="http://www.springframework.org/schema/aop"
5. xmlns:tx="http://www.springframework.org/schema/tx"
6. xsi:schemaLocatinotallow="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8. >
9. <!-- 数据源 -->
10. <bean id="dataSource"
11. class="org.apache.commons.dbcp.BasicDataSource"
12. destroy-method="close">
13. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
14. <property name="url"
15. value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
16. <property name="username" value="root" />
17. <property name="password" value="root" />
18. <!-- 连接池启动时的初始值 -->
19. <property name="initialSize" value="10" />
20. <!-- 连接池的最大值 -->
21. <property name="maxActive" value="10" />
22. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
23. <property name="maxIdle" value="20" />
24. <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
25. <property name="minIdle" value="10" />
26. <property name="defaultAutoCommit" value="true" />
27. </bean>
28. <!-- 会话工厂 -->
29. <bean id="sessionFactory"
30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
31. <property name="dataSource" ref="dataSource" />
32. <property name="mappingLocations">
33. <list>
34. <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
35. </list>
36. </property>
37. <property name="hibernateProperties">
38. <props>
39. <prop key="hibernate.dialect">
40. org.hibernate.dialect.MySQL5Dialect
41. </prop>
42. <prop key="hibernate.show_sql">true</prop>
43. <prop key="hibernate.format_sql">true</prop>
44. </props>
45. </property>
46. </bean>
47. <!-- 定义事务管理器 -->
48. <bean id="transactionManager"
49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
50. <property name="sessionFactory" ref="sessionFactory" />
51. </bean>
52. <!-- 配置服务层 -->
53. <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
54. <property name="sessionFactory" ref="sessionFactory" />
55. </bean>
56. <bean id="userDao"
57. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
58. <!-- 配置事务管理器 -->
59. <property name="transactionManager" ref="transactionManager" />
60. <property name="target" ref="userDaoAgency" />
61. <property name="proxyInterfaces" value="com.dao.UserDao" />
62. <!-- 配置事务属性 -->
63. <property name="transactionAttributes">
64. <props>
65. <prop key="*">PROPAGATION_REQUIRED</prop>
66. </props>
67. </property>
68. </bean>
69. </beans>
第二种:所有Bean共享一个代理:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/aop"
5. "http://www.springframework.org/schema/tx"
6. //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
9. <!-- 数据源 -->
10. "dataSource"
11. class="org.apache.commons.dbcp.BasicDataSource"
12. "close">
13. "driverClassName" value="com.mysql.jdbc.Driver"
14. "url"
15. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
16. "username" value="root"
17. "password" value="root"
18. <!-- 连接池启动时的初始值 -->
19. "initialSize" value="10"
20. <!-- 连接池的最大值 -->
21. "maxActive" value="10"
22. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
23. "maxIdle" value="20"
24. <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
25. "minIdle" value="10"
26. "defaultAutoCommit" value="true"
27. </bean>
28. <!-- 会话工厂 -->
29. "sessionFactory"
30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
31. "dataSource" ref="dataSource"
32. "mappingLocations">
33. <list>
34. /**/*.hbm.xml</value>
35. </list>
36. </property>
37. "hibernateProperties">
38. <props>
39. "hibernate.dialect">
40. org.hibernate.dialect.MySQL5Dialect
41. </prop>
42. "hibernate.show_sql">true</prop>
43. "hibernate.format_sql">true</prop>
44. </props>
45. </property>
46. </bean>
47. <!-- 定义事务管理器 -->
48. "transactionManager"
49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
50. "sessionFactory" ref="sessionFactory"
51. </bean>
52. <!-- 定义事务 -->
53. "base"
54. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
55. "true" abstract="true">
56. <!-- 配置事务管理器 -->
57. "transactionManager" ref="transactionManager"
58. <!-- 配置事务属性 -->
59. "transactionAttributes">
60. <props>
61. "*">PROPAGATION_REQUIRED</prop>
62. </props>
63. </property>
64. </bean>
65. <!-- 配置服务层 -->
66. "userDao"
67. class="com.dao.impl.UserDaoImpl">
68. "sessionFactory" ref="sessionFactory"
69. </bean>
70. <!-- 代理对象 -->
71. "userDaoAgency" parent="base">
72. "target" ref="userDao"
73. </bean>
74. </beans>
第三种:拦截器:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/aop"
5. "http://www.springframework.org/schema/tx"
6. //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
7. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
8. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
9. <!-- 数据源 -->
10. "dataSource"
11. class="org.apache.commons.dbcp.BasicDataSource"
12. "close">
13. "driverClassName" value="com.mysql.jdbc.Driver"
14. "url"
15. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
16. "username" value="root"
17. "password" value="root"
18. <!-- 连接池启动时的初始值 -->
19. "initialSize" value="10"
20. <!-- 连接池的最大值 -->
21. "maxActive" value="10"
22. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
23. "maxIdle" value="20"
24. <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
25. "minIdle" value="10"
26. "defaultAutoCommit" value="true"
27. </bean>
28. <!-- 会话工厂 -->
29. "sessionFactory"
30. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
31. "dataSource" ref="dataSource"
32. "mappingLocations">
33. <list>
34. /**/*.hbm.xml</value>
35. </list>
36. </property>
37. "hibernateProperties">
38. <props>
39. "hibernate.dialect">
40. org.hibernate.dialect.MySQL5Dialect
41. </prop>
42. "hibernate.show_sql">true</prop>
43. "hibernate.format_sql">true</prop>
44. </props>
45. </property>
46. </bean>
47. <!-- 定义事务管理器(声明式的事务) -->
48. "transactionManager"
49. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
50. "sessionFactory" ref="sessionFactory"
51. </bean>
52. <!-- 定义事务 -->
53. "transactionInterceptor"
54. class="org.springframework.transaction.interceptor.TransactionInterceptor">
55. "transactionManager" ref="transactionManager"
56. <!-- 配置事务属性 -->
57. "transactionAttributes">
58. <props>
59. "*">PROPAGATION_REQUIRED</prop>
60. </props>
61. </property>
62. </bean>
63. class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
64. "beanNames">
65. <list>
66. <value>*DaoImpl</value>
67. </list>
68. </property>
69. "interceptorNames">
70. <list>
71. <value>transactionInterceptor</value>
72. </list>
73. </property>
74. </bean>
75. <!-- 配置服务层 -->
76. "userDaoAgency" class="com.dao.impl.UserDaoImpl">
77. "sessionFactory" ref="sessionFactory"
78. </bean>
79. </beans>
第四种:使用tx标签配置的拦截器:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/context"
5. "http://www.springframework.org/schema/aop"
6. "http://www.springframework.org/schema/tx"
7. //www.springframework.org/schema/beans
8. //www.springframework.org/schema/beans/spring-beans-2.5.xsd
9. //www.springframework.org/schema/context
10. //www.springframework.org/schema/context/spring-context-2.5.xsd
11. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13. <!-- 数据源 -->
14. "dataSource"
15. class="org.apache.commons.dbcp.BasicDataSource"
16. "close">
17. "driverClassName" value="com.mysql.jdbc.Driver"
18. "url"
19. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
20. "username" value="root"
21. "password" value="root"
22. <!-- 连接池启动时的初始值 -->
23. "initialSize" value="10"
24. <!-- 连接池的最大值 -->
25. "maxActive" value="10"
26. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
27. "maxIdle" value="20"
28. <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
29. "minIdle" value="10"
30. "defaultAutoCommit" value="true"
31. </bean>
32. <!-- 会话工厂 -->
33. "sessionFactory"
34. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
35. "dataSource" ref="dataSource"
36. "mappingLocations">
37. <list>
38. /**/*.hbm.xml</value>
39. </list>
40. </property>
41. "hibernateProperties">
42. <props>
43. "hibernate.dialect">
44. org.hibernate.dialect.MySQL5Dialect
45. </prop>
46. "hibernate.show_sql">true</prop>
47. "hibernate.format_sql">true</prop>
48. </props>
49. </property>
50. </bean>
51. <context:annotation-config />
52. package="com.dao"
53. <!-- 定义事务管理器 -->
54. "transactionManager"
55. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
56. "sessionFactory" ref="sessionFactory"
57. </bean>
58. <!-- 定义事务 -->
59. "txAdvice" transaction-manager="transactionManager">
60. <tx:attributes>
61. "*" propagatinotallow="REQUIRED"
62. </tx:attributes>
63. </tx:advice>
64. <!-- 定义切面 -->
65. <aop:config>
66. "interceptorPointCuts" expressinotallow="execution(* com.dao.*.*(..))"
67. "txAdvice" pointcut-ref="interceptorPointCuts"
68. </aop:config>
69. </beans>
第五种:注解:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/context"
5. "http://www.springframework.org/schema/aop"
6. "http://www.springframework.org/schema/tx"
7. //www.springframework.org/schema/beans
8. //www.springframework.org/schema/beans/spring-beans-2.5.xsd
9. //www.springframework.org/schema/context
10. //www.springframework.org/schema/context/spring-context-2.5.xsd
11. //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12. //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13. <!-- 数据源 -->
14. "dataSource"
15. class="org.apache.commons.dbcp.BasicDataSource"
16. "close">
17. "driverClassName" value="com.mysql.jdbc.Driver"
18. "url"
19. "jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8"
20. "username" value="root"
21. "password" value="root"
22. <!-- 连接池启动时的初始值 -->
23. "initialSize" value="10"
24. <!-- 连接池的最大值 -->
25. "maxActive" value="10"
26. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
27. "maxIdle" value="20"
28. <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
29. "minIdle" value="10"
30. "defaultAutoCommit" value="true"
31. </bean>
32. <!-- 会话工厂 -->
33. "sessionFactory"
34. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
35. "dataSource" ref="dataSource"
36. "mappingLocations">
37. <list>
38. /**/*.hbm.xml</value>
39. </list>
40. </property>
41. "hibernateProperties">
42. <props>
43. "hibernate.dialect">
44. org.hibernate.dialect.MySQL5Dialect
45. </prop>
46. "hibernate.show_sql">true</prop>
47. "hibernate.format_sql">true</prop>
48. </props>
49. </property>
50. </bean>
51. <context:annotation-config />
52. <!-- 使用注解的包路径 -->
53. package="com.dao"
54. @Transactional
55. "transactionManager"/>
56. <!-- 定义事务管理器 -->
57. "transactionManager"
58. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
59. "sessionFactory" ref="sessionFactory"
60. </bean>
61. </beans>
如果使用了注解,那么实现类应该这样写:
1. package
2. import
3. import
4. import
5. import
6. @Transactional
7. @Component("userDaoAgency")
8. public class UserDaoImpl extends HibernateDaoSupport implements
9. /**
10. * 为方法增加事务处理特性
11. */
12. @Transactional(readOnly=true)
13. public void
14. }
15. }
这样每个方法都能自己定义自己的事务处理!