`
george.gu
  • 浏览: 73571 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

AOP(1):应用中的几个小故事

阅读更多

I had heared about AOP almost 7 years but only used it in two projects. 7 Years, I still only know few about AOP, if i were not involved in that two projects, i would still know that few.

The fact is always like that:

 

  1. Hear about it.
  2. Know how to use it.
  3. Use it in project
  4. Know what's problem it solved and try to enhance it.

First story: Why there is no "before" or "after" method execution Advice?

Normally for a AOP, we can say there are point-cuts or events:

 

  1. Befroe execution
  2. Before return
  3. Exception catch and processing in case exception raised

First time, I use AOP with EJB3. Thanks to annotation @Interceptor and @AroudInvoke, i can define AOP interceptors easily. But for a long time during, I was always wondering:

 

  • Why there is only "around " invokea method, but no "before" or "after" invoke methods?
In fact, AOP engine already get the hand from execution environment and give you the hand to execute the methods, so you can do whatever you want: before the method execution, after methods execution or catch and process the exception. Please remember to return the execution result in order to launch the successive interceptor chain or return the control to execution environment.

What's more, in fact there are dedicated before, throws and after returning advice(interceptor) in spring AOP.  

Second story: Exception used as logical condition had been intercepted by prior interceptor

There is one advisor to manage transaction, its main usage as following:
  1. Catch Exception from point-cut and retry execution in case there is exception.
  2. If max retry number catched, marks Transaction to be "Rollback Only".
Here is the snapshot for tx-advice defintion :

  <tx:advice id="tx-advice" transaction-manager="myTransactionManager">

      <tx:attributes>

          <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>

      </tx:attributes>

  </tx:advice>

 

  <bean id="myTransactionManager"

                class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory">

            <ref bean="mySessionFactory"/>

        </property>

  </bean>

 

 

  <bean id="TransactionRetryInterceptor"

        class="com.MyTransactionRetryInterceptor"/>

 

Here is the shanpshot for MyTransactionRetryInterceptor: 

 

    public Object invoke(MethodInvocation invocation) throws Throwable {

        int retryCount = 0;

        logger.info("TRANSACTION BEGIN");

        while (true) {

            try {

                ReflectiveMethodInvocation inv =

                                                                                (ReflectiveMethodInvocation) invocation;

                MethodInvocation anotherInvocation = inv.invocableClone();

                Object obj = anotherInvocation.proceed();

                 logger.info("TRANSACTION END: OK");

                 return obj;

            } catch (Exception ex) {

                // If reached maximum number of retries then just rethrow this exception immediately.

                if (++retryCount > maxRetryCount) {

                      logger.error ("TRANSACTION FAIL: max retries reached:", ex);

                      throw ex;

                }

 

                //TODO...check if the transaction need to be retried, if yes, retry it. Otherwise, // throw the exception directly.

             }

 

             // Clean-up & delay for a while before retrying.

             cleanupBeforeRetrying();

             delay();

        }

    }


I define point-cuts on both class A and class B. Main behavior of B:
  1. call A.dosomething() to execute some logical.
  2. If A.dosomething() raise exception C, that means some special case happen, B should process dedicated use case.
When i launch the application, it always warn Transaction rollback. It makes my confuse for a period. If we study what happen behind, we can get the following procedure:
  1. Class B invoke Class A to do something.
  2. Intercaptor will catch the exception C raised by A and mark Transaction "rollback only" once reach max retry number.
  3. Intercaptor delegate exception C to class B,
  4. Class B executed completely and try to submit the Transaction.
  5. Transaction rollback as it is marked "Rollback Only".
So I have to redesign the business logical:
  1. Do not set point-cut on class A.
  2. Or do not use exception as business logical condition.
  3. Or use some other adapter who provide same behavior but has no that transaction interceptor. It is like first workaround.
Third Story: It is really difficulty to debug impossible NullPointerException raised because of not-well defined interceptor. 
It is like second story, but it makes situation more complex:
  • There are some Interceptors/Advisors which are not defined by myself.
So if other people define new interceptors for the classes I am working on, it will be difficulty to identify the error in integration test stage.


分享到:
评论

相关推荐

    AOP联盟:aopalliance.jar API包

    AOP联盟:aopalliance.jar API包a set of AOP Java interface .

    aop:aspect

    标题 "aop:aspect" 指涉的是Spring框架中的面向切面编程(Aspect-Oriented Programming, AOP)的一个核心概念。在Spring AOP中,`aop:aspect`是XML配置中定义一个切面的元素,它允许我们将关注点(如日志、事务管理...

    aopalliance-1.0.rar

    AOP Alliance是一个在Java生态中广泛使用的开源项目,其1.0版本的发布对于理解和实践面向切面编程(Aspect-Oriented Programming,AOP)具有重要意义。本资源"aopalliance-1.0.rar"包含了这个关键版本的所有内容,是...

    开发工具 aopalliance-1.0

    开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...

    Spring AOP应用Demo

    在Spring AOP中,主要有以下几个关键概念: 1. **切面(Aspect)**:定义了一个关注点的所有组件,包括通知(Advice)、切入点(Pointcut)和织入(Weaving)。 2. **通知(Advice)**:在特定连接点(Join Point...

    Java中的面向切面编程(AOP):深入理解与实践应用

    AOP提供了一种强大的机制,允许开发者将横切...通过Spring AOP或AspectJ,我们可以轻松地在Java应用程序中实现AOP。理解AOP的工作原理和核心概念,可以帮助我们更好地利用这一强大的工具,编写出更加清晰、高效的代码。

    aopalliance源码

    在实际应用中,`MethodInterceptor`通常与`org.springframework.aop.framework.ProxyFactoryBean`或`org.aspectj.lang.ProceedingJoinPoint`等具体实现结合使用。`ProxyFactoryBean`是Spring AOP中的一个关键组件,...

    spring AOP注解的应用1

    在Spring框架中,AOP(面向切面编程)是一种强大的工具,它允许程序员将关注点分离,例如日志记录、事务管理、权限检查等,从核心业务逻辑中解耦出来。本篇主要探讨的是如何利用Spring AOP的注解来实现这些功能,...

    深入解析Spring AOP源码:从原理到实现,全方位掌握Java AOP编程精髓

    在源码解析中,我们通常会关注以下几个关键点: 1. 切面类的解析:如何识别和处理切面类,提取切点和通知信息。 2. 创建代理:根据目标对象的类型选择JDK动态代理或CGLIB代理,并生成相应的代理类。 3. 代理类的调用...

    SpringAOP1.zip

    在实际应用中,Spring AOP还支持多种类型的通知,如前置通知(Before)、后置通知(After)、返回通知(After Returning)、异常通知(After Throwing)和环绕通知(Around)。环绕通知可以通过实现`org.aopalliance...

    spring 应用aop 实例

    1. **配置AOP支持**:在Spring的配置文件(如`applicationContext.xml`)中,开启AOP代理。可以使用`&lt;aop:aspectj-autoproxy/&gt;`标签来启用基于注解的AOP支持。 2. **定义切面**:创建一个Java类,该类代表切面,...

    spring aop spring aop

    在Spring AOP中,主要有以下几个核心概念: 1. **切面(Aspect)**:切面是关注点的模块化,包含pointcut(切点)和advice(通知)两部分。例如,在上述代码中的`PersonProxy`类就是一个切面,它定义了`before()`和...

    spring aop简单应用示例

    本示例将深入探讨Spring AOP的基础知识,以及如何在实际应用中使用它。 首先,我们来看"LogProfilter.java",这很可能是实现一个日志拦截器的类。在Spring AOP中,这样的类通常被称为切面(Aspect)。切面是封装了...

    springAOP中文文档

    1. **启用 AspectJ 注解支持**:为了在 Spring 应用中使用 AspectJ 注解,需要确保项目 classpath 下包含了必要的 AspectJ 类库,包括 `aopalliance.jar`、`aspectjweaver.jar` 等。 2. **配置 AspectJ 切面**:...

    AOP技术在j2ee中的应用

    ### AOP技术在J2EE系统构建中的应用 #### 一、引言 在软件开发过程中,特别是构建大型的企业级应用时,系统中往往会存在一些跨多个模块的功能需求,例如事务管理、日志记录、安全控制等。这些功能通常被称为**横切...

    spring1.x使用AOP实例

    在IT行业中,Spring框架是Java开发中的一个核心组件,尤其在企业级应用中广泛应用。Spring 1.x版本虽然已经较为古老,但理解其AOP(面向切面编程)的使用对于学习Spring框架的整体架构和设计理念至关重要。AOP是...

    Spring_AOP开发

    /beans&gt;四、基于 XML 配置进行 AOP 开发:在 XML 文件中配置切面、通知、切入点等信息,例如:&lt;aop:config&gt;&lt;aop:aspect id="myInterceptor" ref="myInterceptorBean"&gt;&lt;aop:before method="doAccessCheck" pointcut=...

Global site tag (gtag.js) - Google Analytics