• 1、前言
  • 2、嵌套方法拦截失效
    • 2.1 问题场景
    • 2.2 解决方案
    • 2.3 原因分析
      • 2.3.1 原理
      • 2.3.2 源代码分析
  • 3、Spring事务在多线程环境下失效
    • 3.1 问题场景
    • 3.2 解决方案
    • 3.3 原因分析
  • 4、总结

1、前言

Spring AOP在使用过程中需要注意一些问题,也就是平时我们说的陷阱,这些陷阱的出现是由于Spring AOP的实现方式造成的。对于这些缺陷本人坚持的观点是:一是每一样技术都或多或少有它的局限性,很难称得上完美,只要掌握其实现原理,在使用时不要掉进陷阱就行,也就是进行规避;二是更进一步讲,我们应该接受这就是技术本身的特点,也说不上什么缺陷,它本身就在“那里”,只是我们要的结果是“这样”,而它表现的是“那样”,恰好不是我们想要的而已。

对于Spring AOP的陷阱,我总结了以下两个方面,现在分别进行介绍。

2、嵌套方法拦截失效

2.1 问题场景

通过例子来讲解这样更好,首先加上注解配置:

  1. <!-- 启用注解式AOP -->
    <aop:aspectj-autoproxy/>

然后定义一个切面,代码如下:

  1. @Aspect
  2. @Component
  3. public class AnnotationAspectTest {
  4.  
  5. @Pointcut("execution(* *.action(*))")
  6. public void action() {
  7. }
  8.  
  9. @Pointcut("execution(* *.work(*))")
  10. public void work() {
  11. }
  12.  
  13. @Pointcut("action() || work())")
  14. public void compositePointcut() {
  15. }
  16.  
  17. //前置通知
  18. @Before("compositePointcut()")
  19. public void beforeAdvice() {
  20. System.out.println("before advice.................");
  21. }
  22.  
  23. //后置通知
  24. @After("compositePointcut()")
  25. public void doAfter() {
  26. System.out.println("after advice..................");
  27. }
  28. }

测试代码:

  1. //定义接口
  2. public interface IPersonService {
  3. String action(String msg);
  4.  
  5. String work(String msg);
  6. }
  7.  
  8. //编写实现类
  9. @Service
  10. public class PersonServiceImpl implements IPersonService {
  11.  
  12. public String action(String msg) {
  13. System.out.println("FooService, method doing.");
  14.  
  15. this.work(msg); // *** 代码 1 ***
  16.  
  17. return "[" + msg + "]";
  18. }
  19.  
  20. @Override
  21. public String work(String msg) {
  22. System.out.println("work: * " + msg + " *");
  23. return "* " + msg + " *";
  24. }
  25. }
  26.  
  27. //单元测试
  28. @RunWith(SpringJUnit4ClassRunner.class)
  29. @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
  30. public class FooServiceTest {
  31.  
  32. @Autowired
  33. private IPersonService personService;
  34.  
  35. @Test
  36. public void testAction() {
  37. personService.action("hello world.");
  38. }
  39. }

测试结果:

说明嵌套在action方法内部的work方法没有被进行切面增强,它没有被“切中”。

2.2 解决方案

在实现类中,如果注释掉代码1,将代码1改为:

  1. ((IPersonService) AopContext.currentProxy()).work(msg); // *** 代码 2 ***

并且在XML配置中加上expose-proxy="true",变为:<aop:aspectj-autoproxy expose-proxy="true"/>

运行结果为:

嵌套在action方法内部的work方法被进行了切面增强,它被“切中”。

2.3 原因分析

2.3.1 原理

以上结果的出现与Spring AOP的实现原理息息相关,由于Spring AOP采用了动态代理实现AOP,在Spring容器中的bean(也就是目标对象)会被代理对象代替,代理对象里加入了我们需要的增强逻辑,当调用代理对象的方法时,目标对象的方法就会被拦截。而上文中问题出现的症结也就是在这里,为了进一步说明这个问题,用图片说明最好:

通过调用代理对象的action方法,在其内部会经过切面增强,然后方法被发射到目标对象,在目标对象上执行原有逻辑,如果在原有逻辑中嵌套调用了work方法,则此时work方法并没有被进行切面增强,因为此时它已经在目标对象内部。

而解决方案很好地说明了,将嵌套方法发射到代理对象,这样就完成了切面增强。

2.3.2 源代码分析

接下来我们简单看一下源代码,Spring AOP的代码逻辑相当清晰:

  1. /**
  2. * Implementation of {@code InvocationHandler.invoke}.
  3. * <p>Callers will see exactly the exception thrown by the target,
  4. * unless a hook method throws an exception.
  5. */
  6. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  7. ... ...
  8.  
  9. Object retVal;
  10.  
  11. //*** 代码3 ***
  12. if (this.advised.exposeProxy) {
  13. // Make invocation available if necessary.
  14. oldProxy = AopContext.setCurrentProxy(proxy);
  15. setProxyContext = true;
  16. }
  17.  
  18. ... ...
  19.  
  20. // Get the interception chain for this method.
  21. List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
  22.  
  23. // Check whether we have any advice. If we don't, we can fallback on direct
  24. // reflective invocation of the target, and avoid creating a MethodInvocation.
  25. if (chain.isEmpty()) {
  26. // We can skip creating a MethodInvocation: just invoke the target directly
  27. // Note that the final invoker must be an InvokerInterceptor so we know it does
  28. // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
  29. retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
  30. }
  31. else {
  32. // We need to create a method invocation...
  33. invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
  34. // Proceed to the joinpoint through the interceptor chain.
  35. retVal = invocation.proceed();
  36. }
  37.  
  38. ... ...
  39. }

在代码3处,如果配置了exposeProxy开关,则会将代理对象暴露在当前线程中,以供其它需要的地方使用。那么是怎么暴露的呢?答案很简单,通过使用静态的全局ThreadLocal变量就解决了问题。

3、Spring事务在多线程环境下失效

3.1 问题场景

沿用上面的代码稍作修改,加上事务配置:

  1. <!-- 数据库的事务管理器配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="meilvDataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

代码如下所示:

  1. @Service
  2. @Transactional(propagation = Propagation.REQUIRED, timeout = 10000000)
  3. public class PersonServiceImpl implements IPersonService {
  4.  
  5. @Autowired
  6. IUserDAO userDAO;
  7.  
  8. @Override
  9. public String action(final String msg) {
  10.  
  11. new Thread(new Runnable() {
  12. @Override
  13. public void run() {
  14. (getThis()).work(msg);
  15. }
  16. }).start();
  17.  
  18. UserDO userDO = new UserDO();
  19. userDO.setName("lanlan");
  20. userDAO.insert(userDO);
  21.  
  22. return "[" + msg + "]";
  23. }
  24.  
  25. @Override
  26. public String work(String msg) {
  27. System.out.println("work: * " + msg + " *");
  28. UserDO userDO = new UserDO();
  29. userDO.setName("yanyan");
  30. userDAO.insert(userDO);
  31.  
  32. throw new RuntimeException();
  33. }
  34.  
  35. private IPersonService getThis() {
  36. try {
  37. return (IPersonService) AopContext.currentProxy();
  38. } catch (IllegalStateException e) {
  39. return this;
  40. }
  41. }
  42. }

结果:work方法中抛出异常,但是没有影响事务的回滚,说明事务在子线程中失效了。

3.2 解决方案

只需要将多线程中的方法提出来,或者作为另一个Service类中的方法即可。

  1. @Service
  2. @Transactional(propagation = Propagation.REQUIRED, timeout = 10000000)
  3. public class PersonServiceImpl implements IPersonService {
  4.  
  5. @Autowired
  6. IUserDAO userDAO;
  7.  
  8. @Override
  9. public String action(final String msg) {
  10.  
  11. (getThis()).work(msg);
  12.  
  13. UserDO userDO = new UserDO();
  14. userDO.setName("lanlan");
  15. userDAO.insert(userDO);
  16.  
  17. return "[" + msg + "]";
  18. }
  19.  
  20. @Override
  21. public String work(String msg) {
  22. System.out.println("work: * " + msg + " *");
  23. UserDO userDO = new UserDO();
  24. userDO.setName("yanyan");
  25. userDAO.insert(userDO);
  26.  
  27. throw new RuntimeException();
  28. }
  29.  
  30. private IPersonService getThis() {
  31. try {
  32. return (IPersonService) AopContext.currentProxy();
  33. } catch (IllegalStateException e) {
  34. return this;
  35. }
  36. }
  37. }

上面只是一个简单的例子,用于进行问题说明。

a、如果去掉多线程,将方法放在同一个类里,Spring则会根据事务的传播配置参数,是否重新启用新的事务。

b、如果将方法独立出来放在新的类里,并且该方法也配置了事务,则会重新启用新的事务。

3.3 原因分析

Spring的事务处理为了与数据访问解耦,它提供了一套处理数据资源的机制,而这个机制与上文中的原理相差无几,也是采用的ThreadLocal的方式。

在编程中,Service实例都是单例的无状态的,事务管理则需要加入事务控制的相关状态变量,使得Service实例不再是无状态线程安全的,解决这个问题的方式就是使用ThreadLocal。

通过使用ThreadLocal将数据源绑定在当前线程上,在当前线程的事务中,从设定的地方去取连接就会是同一个数据库连接,这样操作事务就会在同一个连接上进行。

如下图所示:

但是,ThreadLocal的特性是,绑定在当前线程中的变量不会自动传递到其它线程中(当然,InheritableThreadLocal可以在父子线程中间传递变量值,但是这需要特殊的使用场景),所以当开启子线程时,子线程并没有父线程的数据库连接资源。

对于上文提到的陷阱:如果另外开启线程,那么在新线程中将获取不到父线程的连接,事务要么失效,要么重新开启一个新的。

源代码如下:

  1. public abstract class DataSourceUtils {
  2.  
  3. public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
  4. try {
  5. return doGetConnection(dataSource);
  6. }
  7. catch (SQLException ex) {
  8. throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
  9. }
  10. }
  11.  
  12. public static Connection doGetConnection(DataSource dataSource) throws SQLException {
  13. ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
  14. if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
  15. conHolder.requested();
  16. if (!conHolder.hasConnection()) {
  17. logger.debug("Fetching resumed JDBC Connection from DataSource");
  18. conHolder.setConnection(dataSource.getConnection());
  19. }
  20. return conHolder.getConnection();
  21. }
  22.  
  23. Connection con = dataSource.getConnection();
  24.  
  25. ......
  26.  
  27. return con;
  28. }
  29. }
  30.  
  31. public abstract class TransactionSynchronizationManager {
  32.  
  33. private static final ThreadLocal<Map<Object, Object>> resources =
  34. new NamedThreadLocal<Map<Object, Object>>("Transactional resources");
  35.  
  36. /**
  37. * Retrieve a resource for the given key that is bound to the current thread.
  38. * @param key the key to check (usually the resource factory)
  39. * @return a value bound to the current thread (usually the active
  40. * resource object), or {@code null} if none
  41. * @see ResourceTransactionManager#getResourceFactory()
  42. */
  43. public static Object getResource(Object key) {
  44. Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
  45. Object value = doGetResource(actualKey);
  46. if (value != null && logger.isTraceEnabled()) {
  47. logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" +
  48. Thread.currentThread().getName() + "]");
  49. }
  50. return value;
  51. }
  52.  
  53. /**
  54. * Actually check the value of the resource that is bound for the given key.
  55. */
  56. private static Object doGetResource(Object actualKey) {
  57. Map<Object, Object> map = resources.get();
  58. if (map == null) {
  59. return null;
  60. }
  61. Object value = map.get(actualKey);
  62. // Transparently remove ResourceHolder that was marked as void...
  63. if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) {
  64. map.remove(actualKey);
  65. // Remove entire ThreadLocal if empty...
  66. if (map.isEmpty()) {
  67. resources.remove();
  68. }
  69. value = null;
  70. }
  71. return value;
  72. }
  73. }

4、总结

本文总结了Spring AOP和事务的两个陷阱,在平时的实际开发中经常与遇到,只有深入了解了其中的原理,才会在工作中能够有效应对。

Spring AOP和事务的相关陷阱的更多相关文章

  1. Web基础之Spring AOP与事务

    Spring之AOP AOP 全程Aspect Oriented Programming,直译就是面向切面编程.和POP.OOP相似,它也是一种编程思想.OOP强调的是封装.继承.多态,也就是功能的模 ...

  2. Spring AOP及事务配置三种模式详解

    Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...

  3. 如何简单理解spring aop和事务

    用比喻的方法理解吧: 初学者的理解,仅仅为了个人好记 aop:由三部分组成:工具箱,工人,为工人分配工具 tx事务:由四部分组成:管理者,制度,工人,向工人通知管理制度  为什么这样理解呢?个人觉得好 ...

  4. spring AOP 实现事务和主从读写分离

    1 切面 是个类 2 切入点 3 连接点 4 通知 是个方法 5 配置文件 <?xml version="1.0" encoding="UTF-8"?&g ...

  5. Spring AOP (事务管理)

    一.声明式事务管理的概括 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的方式之一. Spring的声明式事务顾名思义就是采用声明 ...

  6. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  7. Java--通过Spring AOP进行事务管理

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. Spring框架的事务管理相关的类和API

    1. PlatformTransactionManager接口 -- 平台事务管理器.(真正管理事务的类).该接口有具体的实现类,根据不同的持久层框架,需要选择不同的实现类! 2. Transacti ...

  9. Spring AOP 管理事务

    <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* ...

随机推荐

  1. vuex中store分文件时候index.js进行文件整合

    import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); import getters from './getters.js' impo ...

  2. Lucene用法10个小结 (zhuan)

    http://www.cfanz.cn/index.PHP?c=article&a=read&id=303149 *********************************** ...

  3. Win7-U盘安装出现"We were unable to copy your files. "

    使用Windows 7 USB/DVD Download Tool时,提示We were unable to copy your files. Please check your USB device ...

  4. java POi excel 写入大批量数据

    直接贴代码: package jp.co.misumi.mdm.batch.common.jobrunner; import java.io.File; import java.io.FileNotF ...

  5. HTML5 + AJAX ( jQuery版本 ) 文件上传带进度条

    页面技术:HTML5 + AJAX ( jQuery) 后台技术:Servlet 3.0 服务器:Tomcat 7.0 jQuery版本:1.9.1 Servlet 3.0 代码 package or ...

  6. PHP——字符串

    <?php //strlen("aaa");取字符串的长度 *** echo strlen("aaaaa"); echo "<br /&g ...

  7. 《高性能MySQL》读书笔记(1)

    慢查询 当一个资源变得效率低下的时候,应该了解一下为什么会这样.有如下可能原因:1.资源被过度使用,余量已经不足以正常工作.2.资源没有被正确配置3.资源已经损坏或者失灵 因为慢查询,太多查询的实践过 ...

  8. Lua中的常用语句结构以及函数

     1.Lua中的常用语句结构介绍 --if 语句结构,如下实例: gTable = {} ] ] then ]) == gTable[] then ]) else print("unkown ...

  9. imx6 uboot splash image

    跟踪uboot代码,了解imx6 splash image的生成过程. 涉及文件: ./cpu/arm_cortexa8/start.S ./board/freescale/mx6q_sabresd/ ...

  10. 【BZOJ】1053: [HAOI2007]反素数ant(贪心+dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1053 约数个数等于分解出的质因数的(指数+1)的乘积这个就不用说了吧... 然后好神的题在于贪心.. ...