首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring 事宜配置方法

2012-09-09 
spring 事务配置方法Spring配置文件中关于Spring事务配置总是由三个组成部分,分别是DataSource、Transactio

spring 事务配置方法

Spring配置文件中关于Spring事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager。

具体如下图:


根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

第一种方式:每个Bean都有一个代理

  1. <?xml?version="1.0"?encoding="UTF-8"?>? <beans?xmlns="http://www.springframework.org/schema/beans"?
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ????xmlns:context="http://www.springframework.org/schema/context"?
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"? ????xsi:schemaLocation="http://www.springframework.org/schema/beans ?
  4. ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ? ???????????http://www.springframework.org/schema/context ?
  5. ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ? ???????????http://www.springframework.org/schema/aop??????????????????????????????????????? http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">? ????<bean?id="sessionFactory"? ?
  6. ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">? ? ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>? ?
  7. ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>? ????</bean>??? ????<!--?定义事务管理器(声明式的事务)?-->? ?
  8. ????<bean?id="transactionManager"? ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">?
  9. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>? ????</bean>? ????<!--?配置DAO?-->?
  10. ????<bean?id="userDaoTarget"?class="com.bluesky.spring.dao.UserDaoImpl">? ????????<property?name="sessionFactory"?ref="sessionFactory"?/>?
  11. ????</bean>?
  12. ????<bean?id="userDao"? ? ????????class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">? ?
  13. ???????????<!--?配置事务管理器?-->? ? ???????????<property?name="transactionManager"?ref="transactionManager"?/>???? ?
  14. ????????<property?name="target"?ref="userDaoTarget"?/>? ? ?????????<property?name="proxyInterfaces"?value="com.bluesky.spring.dao.GeneratorDao"?/>?
  15. ????????<!--?配置事务属性?-->? ? ????????<property?name="transactionAttributes">? ?
  16. ????????????<props>? ? ????????????????<prop?key="*">PROPAGATION_REQUIRED</prop>?
  17. ????????????</props>? ? ????????</property>? ?
  18. ????</bean>? ? </beans>?

第二种方式:所有Bean共享一个代理基类

  1. <?xml?version="1.0"?encoding="UTF-8"?>? <beans?xmlns="http://www.springframework.org/schema/beans"?
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ????xmlns:context="http://www.springframework.org/schema/context"?
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"? ????xsi:schemaLocation="http://www.springframework.org/schema/beans ?
  4. ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ? ???????????http://www.springframework.org/schema/context ?
  5. ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ? ???????????http://www.springframework.org/schema/aop??????????????????????????????? http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">? ????<bean?id="sessionFactory"? ?
  6. ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">? ? ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>? ?
  7. ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>? ????</bean>??? ????<!--?定义事务管理器(声明式的事务)?-->? ?
  8. ????<bean?id="transactionManager"? ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">?
  9. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>? ????</bean>? ????<bean?id="transactionBase"? ?
  10. ????????????class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"? ? ????????????lazy-init="true"?abstract="true">? ?
  11. ????????<!--?配置事务管理器?-->? ? ????????<property?name="transactionManager"?ref="transactionManager"?/>? ?
  12. ????????<!--?配置事务属性?-->? ? ????????<property?name="transactionAttributes">? ?
  13. ????????????<props>? ? ????????????????<prop?key="*">PROPAGATION_REQUIRED</prop>? ?
  14. ????????????</props>? ? ????????</property>? ?
  15. ????</bean>?
  16. ????<!--?配置DAO?-->? ????<bean?id="userDaoTarget"?class="com.bluesky.spring.dao.UserDaoImpl">?
  17. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>? ????</bean>? ????<bean?id="userDao"?parent="transactionBase"?>? ?
  18. ????????<property?name="target"?ref="userDaoTarget"?/>?? ? ????</bean>?
  19. </beans>?

第三种方式:使用拦截器

  1. <?xml?version="1.0"?encoding="UTF-8"?>? <beans?xmlns="http://www.springframework.org/schema/beans"?
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ????xmlns:context="http://www.springframework.org/schema/context"?
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"? ????xsi:schemaLocation="http://www.springframework.org/schema/beans ?
  4. ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ? ???????????http://www.springframework.org/schema/context ?
  5. ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ? ???????????http://www.springframework.org/schema/aop??????????????????????????????? http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">? ????<bean?id="sessionFactory"? ?
  6. ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">? ? ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>? ?
  7. ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>? ????</bean>??? ????<!--?定义事务管理器(声明式的事务)?-->? ?
  8. ????<bean?id="transactionManager"? ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">?
  9. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>? ????</bean>? ?
  10. ?? ? ????<bean?id="transactionInterceptor"? ?
  11. ????????class="org.springframework.transaction.interceptor.TransactionInterceptor">? ? ????????<property?name="transactionManager"?ref="transactionManager"?/>? ?
  12. ????????<!--?配置事务属性?-->? ? ????????<property?name="transactionAttributes">? ?
  13. ????????????<props>? ? ????????????????<prop?key="*">PROPAGATION_REQUIRED</prop>? ?
  14. ????????????</props>? ? ????????</property>? ?
  15. ????</bean>?
  16. ????<bean?class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">? ? ????????<property?name="beanNames">? ?
  17. ????????????<list>? ? ????????????????<value>*Dao</value>?
  18. ????????????</list>? ? ????????</property>? ?
  19. ????????<property?name="interceptorNames">? ? ????????????<list>? ?
  20. ????????????????<value>transactionInterceptor</value>? ? ????????????</list>? ?
  21. ????????</property>? ? ????</bean>??? ????<!--?配置DAO?-->?
  22. ????<bean?id="userDao"?class="com.bluesky.spring.dao.UserDaoImpl">? ????????<property?name="sessionFactory"?ref="sessionFactory"?/>?
  23. ????</bean>? </beans>?

第四种方式:使用tx标签配置的拦截器

  1. <?xml?version="1.0"?encoding="UTF-8"?>? <beans?xmlns="http://www.springframework.org/schema/beans"?
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ????xmlns:context="http://www.springframework.org/schema/context"?
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"? ????xmlns:tx="http://www.springframework.org/schema/tx"?
  4. ????xsi:schemaLocation="http://www.springframework.org/schema/beans ? ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ?
  5. ???????????http://www.springframework.org/schema/context ? ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ?
  6. ???????????http://www.springframework.org/schema/aop??????????????????????????????? http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ? ???????????http://www.springframework.org/schema/tx????????????????????????????????????? http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">? ????<context:annotation-config?/>?
  7. ????<context:component-scan?base-package="com.bluesky"?/>?
  8. ????<bean?id="sessionFactory"? ? ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">? ?
  9. ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>? ? ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>?
  10. ????</bean>???
  11. ????<!--?定义事务管理器(声明式的事务)?-->? ? ????<bean?id="transactionManager"?
  12. ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">? ????????<property?name="sessionFactory"?ref="sessionFactory"?/>?
  13. ????</bean>?
  14. ????<tx:advice?id="txAdvice"?transaction-manager="transactionManager">? ????????<tx:attributes>?
  15. ????????????<tx:method?name="*"?propagation="REQUIRED"?/>? ????????</tx:attributes>?
  16. ????</tx:advice>?
  17. ????<aop:config>? ????????<aop:pointcut?id="interceptorPointCuts"?
  18. ????????????expression="execution(*?com.bluesky.spring.dao.*.*(..))"?/>? ????????<aop:advisor?advice-ref="txAdvice"?
  19. ????????????pointcut-ref="interceptorPointCuts"?/>??????? ? ????</aop:config>????? ?
  20. </beans>?

第五种方式:全注解

  1. <?xml?version="1.0"?encoding="UTF-8"?>? <beans?xmlns="http://www.springframework.org/schema/beans"?
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ????xmlns:context="http://www.springframework.org/schema/context"?
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"? ????xmlns:tx="http://www.springframework.org/schema/tx"?
  4. ????xsi:schemaLocation="http://www.springframework.org/schema/beans ? ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ?
  5. ???????????http://www.springframework.org/schema/context ? ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ?
  6. ???????????http://www.springframework.org/schema/aop??????????????????????????????? http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ? ???????????http://www.springframework.org/schema/tx????????????????????????????????????? http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">? ????<context:annotation-config?/>?
  7. ????<context:component-scan?base-package="com.bluesky"?/>?
  8. ????<tx:annotation-driven?transaction-manager="transactionManager"/>?
  9. ????<bean?id="sessionFactory"? ? ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">? ?
  10. ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>? ? ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>?
  11. ????</bean>???
  12. ????<!--?定义事务管理器(声明式的事务)?-->? ? ????

热点排行