您的位置:首页 > 编程语言 > Java开发

使用Spring的XML声明式事务管理

2017-06-30 15:23 441 查看
  1、设置applicationContext.xml中的XML的命名空间

  <?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:aop="http://www.springframework.org/schema/aop"

     xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

     xsi:schemaLocation="

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

     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

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

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

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

     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

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

  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  2、加载依赖

      <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aspects</artifactId>

    <version>4.3.9.RELEASE</version>

</dependency> 

  3、声明一个事务管理器 

  applicationContext.xml:

      <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

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

</bean>

4、配置事务管理策略

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

  <tx:attributes >

  <tx:method name="get*" read-only="true" propagation="SUPPORTS" isolation="DEFAULT"/>

  <tx:method name="Save*" propagation="REQUIRED" isolation="DEFAULT"/>

  </tx:attributes>

  </tx:advice>

5、配置AOP

    <aop:config>

    <aop:pointcut expression="execution(* cn.com.bochy.dao.*.*(..))" id="poincut"/>

    <aop:advisor advice-ref="advices" pointcut-ref="poincut"/>

  </aop:config>

  

6、session从当前线程中拿
   session=sf.getCurrentSession();
   不可以使用Opensession() 
   

7、  设置current_session_context_class(高版本的Spring可不配)

在hibernate.cfg.xml中,设置

     <property name="current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</property>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AOP 事务 xml spring hibernate
相关文章推荐