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

Measuring the execution time of methods using an around advice


An around advice is the most powerful type of advice; it can completely replace the target method by some different code. In this recipe, we will use it only to execute some extra code before and after the target method. With the before code, we will get the current time. With the after code, we will get the current time again, and will compare it to the previous time to calculate the total time the target method took to execute. Our target methods will be the controller methods of the controller classes in the controller package.

Getting ready

We will use the aspect class defined in the previous recipe, Creating a Spring AOP aspect class.

How to do it…

Here are the steps for measuring the execution time of controller methods:

  1. In the aspect class, create an advice method annotated with @Around and take ProceedingJoinPoint as an argument:

    @Around("execution(* com.spring_cookbook.controllers.*.*(..))")
    public Object doBasicProfiling...