Book Image

Spring Cookbook

Book Image

Spring Cookbook

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting the execution order of the aspects


When using several aspect classes, it can be necessary to set the order in which the aspects are executed. In this recipe, we will use two aspect classes with before advices targeting controller methods.

Getting ready

We will use the configuration from the Creating a Spring AOP aspect class recipe.

We will use these two aspect classes containing an advice, which logs some text when it's executed:

@Component
@Aspect
public class Aspect1 {

  @Before("execution(* com.spring_cookbook.controllers.*.*(..))")
  public void advice1() {  
    System.out.println("advice1");
  }

}

@Component
@Aspect
public class Aspect2 {

  @Before("execution(* com.spring_cookbook.controllers.*.*(..))")
  public void advice2() {  
    System.out.println("advice2");
  }

}

How to do it…

Here are the steps to set the execution order of the two aspect classes:

  1. Add @Order with a number as parameter to the first aspect:

    @Component
    @Aspect
    @Order(1)
    public class Aspect1 {
  2. Add @Order...