Announcement Announcement Module
Collapse
No announcement yet.
Aspect Order Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          Those will lead to AspectJ load-time weaving.

          -Ramnivas

          Comment


          • #6
            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!

            Comment


            • #7
              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).

              Comment


              • #8
                Thanks Marten, I also found a good example with nice example

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

                Comment

                Working...
                X