Results 1 to 8 of 8

Thread: Aspect Order

  1. #1

    Default Aspect Order

    I have the following two aspects

    Code:
    @Aspect
    @Order(1)
    public class AspectA
    {
      @Before("............")
       public void doit() {}
    }
    Code:
    @Aspect
    @Order(2)
    public class AspectB
    {
      @Before(".............")
      public void doit() {}
    }
    I am using load time weaving. Am I correct that Aspect A before advice always executes first before Aspect B before advice?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    It depends. If you use Spring for loadtime weaving and have defined the aspects in the config file then yes, if you use plain AspectJ for loadtime weaving then no.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    To add to Marten:
    - If you use Spring's proxy-based AOP, @Order will work
    - If you use load-time weaving, you need to use 'declare precedence' or @DeclarePrecedence (check AspectJ docs)

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  4. #4

    Default

    Quote Originally Posted by ramnivas View Post
    To add to Marten:
    - If you use Spring's proxy-based AOP, @Order will work
    - If you use load-time weaving, you need to use 'declare precedence' or @DeclarePrecedence (check AspectJ docs)

    -Ramnivas

    Are <context:load-time-weaver /> and aop.xml belong to spring proxy based AOP? I guess I need some confirmation.

  5. #5
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    Those will lead to AspectJ load-time weaving.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  6. #6
    Join Date
    Nov 2012
    Posts
    6

    Default

    I am trying understand the ordering of advices. The @Order annotation seems to be working perfectly fine with @Before and @Around but its working is pretty different in case of @After and @AfterReturning.

    When I gave @Before advice order(1) and @Around order(2) the ordering was working in the desired order. But when I applied order(1) to @After and order(2) to @AfterReturning then order of execution is completly opposite and the advice with the highest order is executed first.

    My application context file looks like this:-
    Code:
        
           <!-- The @AspectJ support is enabled by including the below tag -->     
           <aop:aspectj-autoproxy/>
           
          <bean id="afterReturningAspect"class="com.springtest.aspect.AfterReturningTestAspect" />
          <bean id="afterPartyAspect" class="com.springtest.aspect.AfterTestAspect" />
    and code of aspects like

    Code:
    @Aspect
    @Order(1)
    public class AfterReturningTestAspect {
    
    @AfterReturning(".......")
    public void doit(){
      System.out.println("@AfterReturning executed");
    }
    }

    Code:
    @Aspect
    @Order(2)
    public class AfterTestAspect {
    
    @After(".......")
     public void doSomethin(){
       System.out.println("@After executed");
     }
    
    }
    Could you please let me know what I am doing wrong!

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I suggest a read of chapter 6 of the reference guide... THe order is the aspect NOT the @After/@Before or whatever is the aspect.

    An aspect can have as manu @Before/@After as you wish. If you have an aspect with order 1 and a before and after advice the before is the FIRST to execute and the after is the LAST to execute (the after is executed in the inverse order as compared to before).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Nov 2012
    Posts
    6

    Default

    Thanks Marten, I also found a good example with nice example

    http://java-sample-program.blogspot....-ordering.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •