Around Aspect

Learn how to wrap an aspect around an intercepted method call with the @Around annotation.

We'll cover the following

@Around annotation

There are aspects that are executed before and after the intercepted method calls. This lesson looks at another type of aspect, the around aspect, which is executed around the intercepted method call. This kind of aspect is useful if we want to perform a task before the intercepted method starts execution and after the method has returned.

Example

A good example of an around aspect is measuring the time taken by the method call to execute. We can note the time when the method call is intercepted, then allow the intercepted method to execute, and note the time after the method returns. Instead of having two separate annotations, @Before and @After, we can accomplish this task using the more advanced @Around annotation.

We will create a class called ExecutionTimeAspect. To mark it as an aspect and a configuration, we will use the @Aspect and @Configuration annotations as follows:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.