zoukankan      html  css  js  c++  java
  • Spring声明式事务管理配置

    1.导入所需jar包(Spring3.0之后不再一起发布依赖包,要自行下载)

    2.在applicationContext.xml下配置事务管理器Bean

    <!-- 配置事务管理器, -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    3.在配置文件的头部引入<tx>和<aop>命名空间

    <tx>命名空间:

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

    <aop>命名空间:

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

    完整的头部信息:

    <?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:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
                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/beans
                http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.3.xsd
                ">

    4.配置事务属性

    <tx:advice id="daoAdvice" transaction-manager="transactionManager">
          <tx:attributes>
                <tx:method name="delect*" propagation="REQUIRED"/>
                <tx:method name="*" propagation="REQUIRED" read-only="true"/>        
            </tx:attributes>
        </tx:advice>

    transaction-manager="transactionManager"  引入Spring事务管理类

    name="delect*" 要执行(delect开头)的方法名,propagation="REQUIRED"有transaction状态下执行。如果当前没有transaction,就创建新的transaction。

    read-only="true" 设置是否只读。

    5.AOP的配置

    <aop:config>
         <aop:pointcut expression="execution(* dao.*.*(..))" id="daoMethod"/>
         <aop:advisor advice-ref="daoAdvice" pointcut-ref="daoMethod"/>
    </aop:config>

    expression="execution(* dao.*.*(..))" 第一个*代表所有的返回值类型,dao代表包名,第二个*带表所有的类,第三个*代表所有的方法,(..)代表方法的参数。

    6.在需要事务的方法里就不用写事务的开始、事务的提交和关闭session。

    package dao;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.query.Query;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Repository;import entity.News;
    
    @Repository
    @Scope("prototype")
    public class NewsDaoImpl implements NewsDaoIntf{
        
        @Autowired
        @Qualifier("sessionFactory")
        private SessionFactory sf;
        
        private List<News> list=new ArrayList<News>();
        
        @Override
        public List<News> getAllNews() {
            
            Session session=sf.getCurrentSession();
            
            session.getTransaction().begin();
            
            Query query=session.createQuery("from News");
            list=query.getResultList();
            
            session.getTransaction().commit();
    

         return list; } }
  • 相关阅读:
    HTTP断点续传 规格严格
    Java Shutdown 规格严格
    linux 命令源码 规格严格
    JTable调整列宽 规格严格
    linux 多CPU 规格严格
    Hello can not find git path 规格严格
    Kill 规格严格
    拜拜牛人 规格严格
    Swing 规格严格
    Debugging hangs in JVM (on AIX but methodology applicable to other platforms) 规格严格
  • 原文地址:https://www.cnblogs.com/Anlotus/p/5969738.html
Copyright © 2011-2022 走看看