liveBook · Manning

@Around annotation

As explained in Spring Start Here, Second Edition

The @Around annotation marks aspect advice that can run before, after, or instead of an intercepted method and can control whether that method proceeds.

@Around marks advice that wraps selected method calls. Its pointcut expression tells Spring which calls to intercept; the advice can run logic around the intercepted method. section 6.2.1

A ProceedingJoinPoint gives the advice control over what happens next:

  • Call proceed() to invoke the intercepted method; the advice can run before and after that call. section 6.2.1
  • Skip proceed() to prevent the intercepted method from running; the advice executes instead. section 6.2.1

Logging around a service call

An advice targeting methods in the services package logs before and after proceed(). A call to CommentService.publishComment then produces the advice logs around the service’s own log. section 6.2.1

Listing 6.9 Implementing the aspect logic
@Aspect
public class LoggingAspect {
 
  private final Logger logger = 
     Logger.getLogger(LoggingAspect.class.getName());
 
  @Around("execution(* com.example.services.*.*(..))")
  public void log(ProceedingJoinPoint joinPoint) 
      throws Throwable {
 
    logger.info("Method will execute");  #A
    joinPoint.proceed();    #B
    logger.info("Method executed");   #C
  }
}

See the complete LoggingAspect advice and its before, proceed, and after sequence. section 6.2.1

@Around is the most powerful of Spring’s five advice annotations, but aspects are best used when they genuinely help rather than being applied everywhere. section 6.5

How Spring Start Here, Second Edition covers it

How deep it goes
Demonstrates pointcut selection and proceed() control with a logging example, without detailing proxy internals.
Examples
Uses a LoggingAspect around CommentService methods to log before and after a comment is published.
What you can do after
Apply it via Spring AOP to intercept selected calls, add surrounding logic, or prevent the original method from running.
How it connects
Connects to Spring AOP, AspectJ pointcut expressions, ProceedingJoinPoint, and other advice annotations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.