>  기사  >  Java  >  Spring 트랜잭션의 5가지 주입 방법에 대한 자세한 설명 트랜잭션 구성

Spring 트랜잭션의 5가지 주입 방법에 대한 자세한 설명 트랜잭션 구성

Y2J
Y2J원래의
2017-05-02 11:37:321848검색

이 글에서는 주로 Spring 트랜잭션 구성의 5가지 주입 방법을 자세히 소개합니다. 여기에는 특정 참고 가치가 있으며 관심 있는 친구들이 참고할 수 있습니다.

얼마 전 Spring의 트랜잭션 구성에 대해 심층적으로 조사한 적이 있습니다. 이 기간 동안 Spring의 트랜잭션 구성을 구성했지만 이에 대해 명확하게 이해한 적이 없습니다. 이번 연구를 통해 Spring의 트랜잭션 구성은 아이디어가 명확하기만 하면 상대적으로 익히기가 쉽다는 것을 알게 되었습니다.

요약은 다음과 같습니다.

Spring 구성 파일의 트랜잭션 구성은 A가 어디에 있든 항상 DataSource, TransactionManager 및 프록시 메커니즘이라는 세 가지 구성 요소로 구성됩니다. 구성 방법을 사용하면 일반적으로 프록시 메커니즘만 변경됩니다.

DataSource와 TransactionManager는 데이터 액세스 방법에 따라서만 변경됩니다. 예를 들어 데이터 액세스를 위해 최대 절전 모드를 사용하는 경우 DataSource는 실제로 SessionFactory이고 TransactionManager의 구현은 HibernateTransactionManager입니다.

자세한 내용은 다음과 같습니다.

프록시 메커니즘에 따라 5가지 Spring 트랜잭션 구성 방법을 요약합니다. 파일은 다음과 같습니다.

첫 번째 방법: 각 Bean에는 프록시가 있습니다


<?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" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 www.springframework.org/schema/beans/spring-beans-2.5.xsd 
 www.springframework.org/schema/context 
 www.springframework.org/schema/context/spring-context-2.5.xsd 
 www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
 
 <bean id="sessionFactory" 
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
 <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
 </bean> 
 
 <!-- 定义事务管理器(声明式的事务) --> 
 <bean id="transactionManager" 
 class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
 <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 
 <!-- 配置DAO --> 
 <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> 
 <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 
 <bean id="userDao" 
 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
 <!-- 配置事务管理器 --> 
 <property name="transactionManager" ref="transactionManager" /> 
 <property name="target" ref="userDaoTarget" /> 
 <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /> 
 <!-- 配置事务属性 --> 
 <property name="transactionAttributes"> 
 <props> 
 <prop key="*"> PROPAGATION_REQUIRED </prop> 
 </props> 
 </property> 
 </bean> 
 </beans>

두 번째 방법: 모든 Bean은 프록시 기본 클래스를 공유합니다

<?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" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 www.springframework.org/schema/beans/spring-beans-2.5.xsd 
 www.springframework.org/schema/context 
 www.springframework.org/schema/context/spring-context-2.5.xsd 
 www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
 
 <bean id="sessionFactory" 
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
 <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
 </bean> 
 
 <!-- 定义事务管理器(声明式的事务) --> 
 <bean id="transactionManager" 
 class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
 <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 
 <bean id="transactionBase" 
 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
 lazy-init="true" abstract="true"> 
 <!-- 配置事务管理器 --> 
 <property name="transactionManager" ref="transactionManager" /> 
 <!-- 配置事务属性 --> 
 <property name="transactionAttributes"> 
 <props> 
 <prop key="*">PROPAGATION_REQUIRED </prop> 
 </props> 
 </property> 
 </bean> 
 
 <!-- 配置DAO --> 
 <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> 
 <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 
 <bean id="userDao" parent="transactionBase"> 
 <property name="target" ref="userDaoTarget" /> 
 </bean> 
 </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" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 www.springframework.org/schema/beans/spring-beans-2.5.xsd 
 www.springframework.org/schema/context 
 www.springframework.org/schema/context/spring-context-2.5.xsd 
 www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
 
 <bean id="sessionFactory" 
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
 <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
 </bean> 
 
 <!-- 定义事务管理器(声明式的事务) --> 
 <bean id="transactionManager" 
 class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
 <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 
 <bean id="transactionInterceptor" 
 class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
 <property name="transactionManager" ref="transactionManager" /> 
 <!-- 配置事务属性 --> 
 <property name="transactionAttributes"> 
 <props> 
 <prop key="*">PROPAGATION_REQUIRED </prop> 
 </props> 
 </property> 
 </bean> 
 
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
 <property name="beanNames"> 
 <list> 
 <value> *Dao </value> 
 </list> 
 </property> 
 <property name="interceptorNames"> 
 <list> 
 <value> transactionInterceptor </value> 
 </list> 
 </property> 
 </bean> 
 
 <!-- 配置DAO --> 
 <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl"> 
 <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 </beans>

네 번째 방법: tx 태그 구성 인터셉터 사용

<?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 
 www.springframework.org/schema/beans/spring-beans-2.5.xsd 
 www.springframework.org/schema/context 
 www.springframework.org/schema/context/spring-context-2.5.xsd 
 www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
 www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
 
 <context:annotation-config /> 
 <context:component-scan base-package="com.bluesky" /> 
 
 <bean id="sessionFactory" 
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
 <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
 </bean> 
 
 <!-- 定义事务管理器(声明式的事务) --> 
 <bean id="transactionManager" 
 class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
 <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 
 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
 <tx:attributes> 
 <tx:method name="*" propagation="REQUIRED" /> 
 </tx:attributes> 
 </tx:advice> 
 
 <aop:config> 
 <aop:pointcut id="interceptorPointCuts" 
 expression="execution(*com.bluesky.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 
   www.springframework.org/schema/beans/spring-beans-2.5.xsd
   www.springframework.org/schema/context
   www.springframework.org/schema/context/spring-context-2.5.xsd
   www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

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

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

 <bean id="sessionFactory" 
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
 </bean> 

 <!-- 定义事务管理器(声明式的事务) --> 
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
</beans>

이때 DAO에 다음과 같이 @Transactional 주석을 추가해야 합니다.

package com.bluesky.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.bluesky.spring.domain.User;

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

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

위 내용은 Spring 트랜잭션의 5가지 주입 방법에 대한 자세한 설명 트랜잭션 구성의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.