Xml配置实现Spring_Hibernate中的声明式事务管理

前端之家收集整理的这篇文章主要介绍了Xml配置实现Spring_Hibernate中的声明式事务管理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


applicationContext.xml

  1. <beans
  2. xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  16. <!-- 1.加入事务命名空间 -->
  17. <context:annotation-config/>
  18. <context:component-scan base-package="com.spr"/>
  19. <aop:aspectj-autoproxy proxy-target-class="true"/>
  20. <context:property-placeholder location="jdbc.properties"/>
  21. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  22. <property name="driverClassName" value="${jdbc.driverClassName}"/>
  23. <property name="url" value="${jdbc.url}"/>
  24. <property name="username" value="${jdbc.username}"></property>
  25. <property name="password" value="${jdbc.password}"></property>
  26. </bean>
  27.  
  28. <bean id="sessionFactory"
  29. class="org.springframework.orm.hibernate4.LocalSessionfactorybean">
  30. <property name="dataSource">
  31. <ref bean="dataSource"/>
  32. </property>
  33. <property name="annotatedClasses">
  34. <list>
  35. <value>com.spr.vo.Students</value>
  36. </list>
  37. </property>
  38. <property name="hibernateProperties">
  39. <props>
  40. <prop key="hibernate.dalect">
  41. org.hibernate.dialect.MysqLDialect
  42. </prop>
  43. <prop key="hibernate.show_sql">true</prop>
  44. <prop key="hibernate.format_sql">true</prop>
  45. <prop key="hibernate.hbm2ddl.auto">update</prop>
  46. </props>
  47. </property>
  48. </bean>
  49. <!-- 2.声明事务管理器 -->
  50. <bean id="transactionManager"
  51. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  52. <property name="sessionFactory" ref="sessionFactory" />
  53. </bean>
  54. <!-- 3.定义事务通知 -->
  55. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  56. <tx:attributes>
  57. <tx:method name="save*" propagation="required"/><!-- 拦截save*方法 -->
  58. </tx:attributes>
  59. </tx:advice>
  60. <!-- 4.定义切面 -->
  61. <aop:config>
  62. <aop:pointcut expression="execution(public * com.spr.dao.StudentsDAOImpl.*Students(..))" id="bizMethods"/>
  63. <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods"/>
  64. </aop:config>
  65. </beans>
完整源代码下载: 点击打开链接

猜你在找的XML相关文章