Spring declarative transaction management configuration

1. Import the required jar package (the dependency package will not be released together after Spring 3.0, you must download it yourself)

2. Configure the transaction manager bean under applicationContext.xml

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

3. Introduce the <tx> and <aop> namespaces at the head of the configuration file

<tx> namespace:

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> namespace:

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

Complete header information:


<?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. Configure transaction properties


<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" introduces Spring transaction management class

name="delect*" The method name to be executed (beginning with delect), propagation="REQUIRED" is executed in transaction state. If there is no current transaction, create a new transaction.

read-only="true" Set whether to read-only.

5. AOP configuration

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

expression="execution(* dao.*.*(..))" The first * represents all return value types, dao represents the package name, the second * contains all classes in the table, and the third * represents all method, (..) represents the parameters of the method.

6. In a method that requires a transaction, there is no need to write the start of the transaction, the commit of the transaction and the closing of the 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;
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895647&siteId=291194637