Spring 事务机制简述

概述

事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性。
Spring Framework对事务管理提供了一致的抽象,其特点如下:

  • 为不同的事务API提供一致的编程模型,比如JTA(Java Transaction API), JDBC, Hibernate, JPA(Java Persistence API和JDO(Java Data Objects)
  • 支持声明式事务管理,特别是基于注解的声明式事务管理,简单易用
  • 提供比其他事务API如JTA更简单的编程式事务管理API
  • 与spring数据访问抽象的完美集成

编程式事务管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager。对于编程式事务管理,spring推荐使用TransactionTemplate。

Spring声明式事务让我们从复杂的事务处理中得到解脱。使得我们再也无需要去处理获得连接、关闭连接、事务提交和回滚等这些操作。再也无需要我们在与事务相关的方法中处理大量的try…catch…finally代码。我们在使用Spring声明式事务时,有一个非常重要的概念就是事务属性。事务属性通常由事务的传播行为,事务的隔离级别,事务的超时值和事务只读标志组成。我们在进行事务划分时,需要进行事务定义,也就是配置事务的属性。

声明式事务管理建立在AOP之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。声明式事务最大的优点就是不需要通过编程的方式管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明(或通过基于@Transactional注解的方式),便可以将事务规则应用到业务逻辑中。

显然声明式事务管理要优于编程式事务管理,这正是spring倡导的非侵入式的开发方式。声明式事务管理使业务代码不受污染,一个普通的POJO对象,只要加上注解就可以获得完全的事务支持。和编程式事务相比,声明式事务唯一不足地方是,后者的最细粒度只能作用到方法级别,无法做到像编程式事务那样可以作用到代码块级别。但是即便有这样的需求,也存在很多变通的方法,比如,可以将需要进行事务管理的代码块独立为方法等等。

声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解如果同时配置这两种事务管理方式,优先使用基于@Transactional注解的方式。显然基于注解的方式更简单易用,更清爽。

下面分别详细讲解,事务的四种属性,仅供诸位学习参考:

Spring在TransactionDefinition接口中定义这些属性,以供PlatfromTransactionManager使用, PlatfromTransactionManager是spring事务管理的核心接口。

           public interface TransactionDefinition {

int getPropagationBehavior();//返回事务的传播行为。
int getIsolationLevel();//返回事务的隔离级别,事务管理器根据它来控制另外一个事务可以看到本事务内的哪些数据。
int getTimeout();//返回事务必须在多少秒内完成。
boolean isReadOnly();//事务是否只读,事务管理器能够根据这个返回值进行优化,确保事务是只读的。
}

1. TransactionDefinition接口中定义五个隔离级别:

  • TransactionDefinition.ISOLATION_DEFAULT:这是默认值,表示使用底层数据库的默认隔离级别。对大部分数据库而言,通常这值就是TransactionDefinition.ISOLATION_READ_COMMITTED。另外四个与JDBC的隔离级别相对应;
  • TransactionDefinition.ISOLATION_READ_UNCOMMITTED:该隔离级别表示一个事务可以读取另一个事务修改但还没有提交的数据。该级别不能防止脏读,不可重复读和幻读,因此很少使用该隔离级别。比如PostgreSQL实际上并没有此级别。
  • TransactionDefinition.ISOLATION_READ_COMMITTED:该隔离级别表示一个事务只能读取另一个事务已经提交的数据。该级别可以防止脏读,这也是大多数情况下的推荐值。
  • TransactionDefinition.ISOLATION_REPEATABLE_READ:该隔离级别表示一个事务在整个过程中可以多次重复执行某个查询,并且每次返回的记录都相同。该级别可以防止脏读和不可重复读。
  • TransactionDefinition.ISOLATION_SERIALIZABLE:所有的事务依次逐个执行,这样事务之间就完全不可能产生干扰,也就是说,该级别可以防止脏读、不可重复读以及幻读。但是这将严重影响程序的性能。通常情况下也不会用到该级别。

2. 在TransactionDefinition接口中定义了七个事务传播行为:

(1)PROPAGATION_REQUIRED 如果存在一个事务,则支持当前事务。如果没有事务则开启一个新的事务。

(2)PROPAGATION_SUPPORTS 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行。但是对于事务同步的事务管理器,PROPAGATION_SUPPORTS与不使用事务有少许不同。

(3)PROPAGATION_MANDATORY 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。

(4)PROPAGATION_REQUIRES_NEW 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。

(5)PROPAGATION_NOT_SUPPORTED  总是非事务地执行,并挂起任何存在的事务。使用PROPAGATION_NOT_SUPPORTED,也需要使用JtaTransactionManager作为事务管理器。

(6)PROPAGATION_NEVER 总是非事务地执行,如果存在一个活动事务,则抛出异常;

(7)PROPAGATION_NESTED如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务, 则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行。这是一个嵌套事务,需要JDBC 驱动的java.sql.Savepoint类。使用PROPAGATION_NESTED,还需要把PlatformTransactionManager的nestedTransactionAllowed属性设为true;而 nestedTransactionAllowed属性值默认为false;

PROPAGATION_NESTED 与PROPAGATION_REQUIRES_NEW的区别:它们非常类似,都像一个嵌套事务,如果不存在一个活动的事务,都会开启一个新的事务。使用 PROPAGATION_REQUIRES_NEW时,内层事务与外层事务就像两个独立的事务一样,一旦内层事务进行了提交后,外层事务不能对其进行回滚。两个事务互不影响。两个事务不是一个真正的嵌套事务。

PROPAGATION_REQUIRES_NEW 启动一个新的, 不依赖于环境的 "内部" 事务. 这个事务将被完全 commited 或 rolled back 而不依赖于外部事务, 它拥有自己的隔离范围, 自己的锁, 等等. 当内部事务开始执行时, 外部事务将被挂起, 内务事务结束时, 外部事务将继续执行。

另一方面, PROPAGATION_NESTED 开始一个 "嵌套的" 事务,  它是已经存在事务的一个真正的子事务. 套事务开始执行时,  它将取得一个 savepoint. 如果这个嵌套事务失败, 我们将回滚到此 savepoint. 嵌套事务是外部事务的一部分, 只有外部事务结束后它才会被提交。

3. 事务超时

所谓事务超时,就是指一个事务所允许执行的最长时间,如果超过该时间限制但事务还没有完成,则自动回滚事务。在 TransactionDefinition 中以 int 的值来表示超时时间,其单位是秒。默认设置为底层事务系统的超时值,如果底层数据库事务系统没有设置超时值,那么就是none,没有超时限制。

4. 事务只读属性

只读事务用于客户代码只读但不修改数据的情形,只读事务用于特定情景下的优化,比如使用Hibernate的时候。默认为读写事务。

5. 事务回滚规则

指示spring事务管理器回滚一个事务的推荐方法是在当前事务的上下文内抛出异常。spring事务管理器会捕捉任何未处理的异常,然后依据规则决定是否回滚抛出异常的事务。

默认配置下,spring只有在抛出的异常为运行时unchecked异常时才回滚该事务,也就是抛出的异常为RuntimeException的子类(Errors也会导致事务回滚),而抛出checked异常则不会导致事务回滚。可以明确的配置在抛出那些异常时回滚事务,包括checked异常。也可以明确定义那些异常抛出时不回滚事务。

还可以编程性的通过setRollbackOnly()方法来指示一个事务必须回滚,在调用完setRollbackOnly()后你所能执行的唯一操作就是回滚。


@Transactional注解

属性类型描述
valueString可选的限定描述符,指定使用的事务管理器
propagationenum: Propagation可选的事务传播行为设置
isolationenum: Isolation可选的事务隔离级别设置
readOnlyboolean读写或只读事务,默认读写
timeoutint (in seconds granularity)事务超时时间设置
rollbackForClass对象数组,必须继承自Throwable导致事务回滚的异常类数组
rollbackForClassName类名数组,必须继承自Throwable导致事务回滚的异常类名字数组
noRollbackForClass对象数组,必须继承自Throwable不会导致事务回滚的异常类数组
noRollbackForClassName类名数组,必须继承自Throwable不会导致事务回滚的异常类名字数组

用法

@Transactional 可以作用于接口、接口方法、类以及类方法上。当作用于类上时,该类的所有 public 方法将都具有该类型的事务属性,同时,我们也可以在方法级别使用该标注来覆盖类级别的定义。

虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是 Spring 建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。

默认情况下,只有来自外部的方法调用才会被AOP代理捕获,也就是,类内部方法调用本类内部的其他方法并不会引起事务行为,即使被调用方法使用@Transactional注解进行修饰。

@Transactional(readOnly = true)
public class DefaultFooService implements FooService {
 
  public Foo getFoo(String fooName) {
    // do something
  }
 
  // these settings have precedence for this method
  //方法上注解属性会覆盖类注解上的相同属性
  @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
  public void updateFoo(Foo foo) {
    // do something
  }
}


自动提交(AutoCommit)与连接关闭时的是否自动提交

自动提交

默认情况下,数据库处于自动提交模式。每一条语句处于一个单独的事务中,在这条语句执行完毕时,如果执行成功则隐式的提交事务,如果
执行失败则隐式的回滚事务。

对于正常的事务管理,是一组相关的操作处于一个事务之中,因此必须关闭数据库的自动提交模式。不过,这个我们不用担心,spring会将底层连接的自动提交特性设置为false。
org/springframework/jdbc/datasource/DataSourceTransactionManager.java

// switch to manual commit if necessary. this is very expensive in some jdbc drivers,
// so we don't want to do it unnecessarily (for example if we've explicitly
// configured the connection pool to set it already).
if (con.getautocommit()) {
    txobject.setmustrestoreautocommit(true);
    if (logger.isdebugenabled()) {
        logger.debug("switching jdbc connection [" + con + "] to manual commit");
    }
    con.setautocommit(false);
}

有些数据连接池提供了关闭事务自动提交的设置,最好在设置连接池时就将其关闭。但C3P0没有提供这一特性,只能依靠spring来设置。
因为JDBC规范规定,当连接对象建立时应该处于自动提交模式,这是跨DBMS的缺省值,如果需要,必须显式的关闭自动提交。C3P0遵守这一规范,让客户代码来显式的设置需要的提交模式。

连接关闭时的是否自动提交

当一个连接关闭时,如果有未提交的事务应该如何处理?JDBC规范没有提及,C3P0默认的策略是回滚任何未提交的事务。这是一个正确的策略,但JDBC驱动提供商之间对此问题并没有达成一致。
C3P0的autoCommitOnClose属性默认是false,没有十分必要不要动它。或者可以显式的设置此属性为false,这样会更明确。

Spring事务配置的五种方式

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。

具体如下图:

Spring事务配置 (2)


根据代理机制的不同,总结了五种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
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://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
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://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
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://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
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://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
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://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();
    }
   
   
}

关于拦截方法调用其他内部方法无法被拦截问题的解决

拦截器的实现原理很简单,就是动态代理,实现AOP机制。当外部调用被拦截bean的拦截方法时,可以选择在拦截之前或者之后等条件执行拦截方法之外的逻辑,比如特殊权限验证,参数修正等操作。但是如果现在一个需求是,当外部调用拦截bean的时候,不但要执行拦截当前方法,如果当前方法内部同时调用了其他内部方法,也要被拦截。按照目前的拦截器实现逻辑,是无法拦截当前方法内部调用的方法的,这样说有点抽象,看一个代码:

public class BeanA{  
public void method1(){  
method2();  
}  
public void method2(){  
...  
}  
}

当外部调用beanA.method1();的时候,拦截器执行拦截逻辑,执行完毕后进入method1方法执行,当调用method2的时候,拦截器是否会再次拦截?是不会的,这里涉及到拦截器的一个原理,拦截器涉及两个对象,代理对象和原始对象,拦截器所执行的代理对象执行完毕后,当执行method1即进入了原始对象,那么在原始对象中调用method2,是无法进行拦截的。所以很显眼,这样无法满足我们的需求。

在spring的源代码中通过一个增强对象的检查,控制了当前的内部调用是否使用代理来执行,这让人感到无奈。spring的作者们很隐晦的提出避免内部调用的方法。

我们可能会想,在外部调用两次beanA,第一次调用method1,第二次调用method2,这样做可以解决问题,但是这样的直接后果是我们的逻辑代码将变得紊乱,并非所有的场景下都可以通过这样的设计来完成。虽然这是spring官方推荐的避免内部调用的idea。

查看了相关资料,得到了一种方法,即在method1的内部,通过直接获取当前代理对象的方式然后通过代理对象调用method2,这样触发拦截。

看看代码:

    public void method1(){  
        logger.error("1");  
          
        // 如果希望调用的内部方法也被拦截,那么必须用过上下文获取代理对象执行调用,而不能直接内部调用,否则无法拦截  
        if(null != AopContext.currentProxy()){  
            ((NorQuickNewsDAO)AopContext.currentProxy()).method2();  
        }else{  
            method2();  
        }         
    }  
      
    public void method2(){  
        logger.error("2");  
    }

我们显示的调用了AopContext来获取当前代理对象,然后调用其方法,这样做还必须的一个步骤是将当前的代理暴露给线程使用,在配置文件中需要配置一个参数:

 <property name="exposeProxy">  
       <value>true</value>  
 </property>

它是ProxyConfig的一个参数,默认是false,如果不设置这个参数,那么上述java代码将无法获取当前线程中的代理对象。

这种方法可以成功触发拦截,但是也带来了其他问题,比如代码的织入,我们的代码将变得复杂而且晦涩,而且严格要求系统针对于当前的bean必须配置拦截器,否则会因为找不到拦截器而抛出异常。

这样做有什么负面影响?对事务的影响,对安全的影响,现在不得而知,还需要逐步去测试以尝试。


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值