0
0
Spring Bootframework~30 mins

@Around advice for full control in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Using @Around Advice for Full Control in Spring Boot
📖 Scenario: You are building a Spring Boot application where you want to log method execution time and control the method execution flow.
🎯 Goal: Create an @Around advice in a Spring Boot aspect to measure and log the execution time of a service method, and control when the method runs.
📋 What You'll Learn
Create a Spring Boot service class with a method performTask() that returns a string.
Create an aspect class with an @Around advice method.
Use ProceedingJoinPoint to control the method execution inside the advice.
Log the start time, execute the method, log the end time, and calculate the duration.
Return the original method's result from the advice.
💡 Why This Matters
🌍 Real World
Using @Around advice in Spring Boot helps monitor and control method execution, useful for logging, performance measurement, and adding custom behavior around business logic.
💼 Career
Understanding @Around advice is important for backend developers working with Spring Boot to implement cross-cutting concerns like logging, security, and transactions.
Progress0 / 4 steps
1
Create the Service Class with performTask() Method
Create a Spring service class called TaskService with a public method performTask() that returns the string "Task Completed".
Spring Boot
Need a hint?

Use @Service annotation and create a method that returns the exact string.

2
Create the Aspect Class with @Around Advice Method
Create an aspect class called LoggingAspect annotated with @Aspect and @Component. Inside it, declare a method logExecutionTime with @Around advice targeting performTask() method in TaskService. The method should accept a ProceedingJoinPoint parameter.
Spring Boot
Need a hint?

Use @Around with the pointcut expression for performTask() and accept ProceedingJoinPoint as parameter.

3
Add Timing Logic Inside the @Around Advice
Inside the logExecutionTime method, record the start time using System.currentTimeMillis(). Then call joinPoint.proceed() to execute the original method and store its result. After that, record the end time and calculate the duration by subtracting start time from end time. Finally, print the duration using System.out.println and return the original method's result.
Spring Boot
Need a hint?

Use System.currentTimeMillis() before and after proceed() to measure time, then print and return the result.

4
Complete the Aspect to Fully Control Method Execution
Modify the logExecutionTime method to add a print statement before calling joinPoint.proceed() that prints "Starting method execution". Also add a print statement after joinPoint.proceed() that prints "Method execution finished". This shows full control over when the method runs inside the advice.
Spring Boot
Need a hint?

Print messages before and after proceed() to show control over method execution.