Spring事务配置的五种方式(3)

<!-- 定义事务管理器(声明式的事务) --> 
    <bean
       >
        <property ref="sessionFactory" />
    </bean>

<tx:advice transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
   
    <aop:config>
        <aop:pointcut
            expression="execution(* com.linuxidc.spring.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="interceptorPointCuts" />       
    </aop:config>     
</beans>

第五种方式:全注解


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

<context:annotation-config />
    <context:component-scan base-package="com.linuxidc" />

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean 
           > 
        <property value="classpath:hibernate.cfg.xml" /> 
        <property value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean> 

<!-- 定义事务管理器(声明式的事务) --> 
    <bean
       >
        <property ref="sessionFactory" />
    </bean>
   
</beans>

此时在DAO上需加上@Transactional注解,如下:

package com.linuxidc.spring.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.linuxidc.spring.domain.User;

@Transactional
 @Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

public List<User> listUsers() {
        return this.getSession().createQuery("from User").list();
    }
   
   
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/481111d210bfdff591ee6b3593e05ce7.html