📖 Scenario: You are building a simple Spring Boot application that logs messages after a service method runs. This helps track when methods finish and what results they return.
🎯 Goal: Create an aspect class that uses @After to log a message after a method runs, and @AfterReturning to log the returned value from the method.
📋 What You'll Learn
Create a service class with a method getMessage() that returns a string
Create an aspect class with @After advice that logs after getMessage() runs
Add @AfterReturning advice in the aspect to log the returned string from getMessage()
Use Spring AOP annotations and proper pointcut expressions
💡 Why This Matters
🌍 Real World
Logging method execution and returned values helps monitor application behavior and debug issues in real Spring Boot applications.
💼 Career
Understanding Spring AOP annotations like @After and @AfterReturning is essential for backend developers working with Spring Boot to implement cross-cutting concerns like logging and auditing.
Progress0 / 4 steps
1
Create the service class with getMessage() method
Create a Spring service class called SimpleService with a public method getMessage() that returns the string "Hello from service".
Spring Boot
Hint
Use @Service annotation and define getMessage() to return the exact string.
2
Create the aspect class with @After advice
Create an aspect class called LoggingAspect annotated with @Aspect and @Component. Add an @After advice method called afterGetMessage() that runs after SimpleService.getMessage() and logs the message "Method getMessage() finished".
Spring Boot
Hint
Use the pointcut expression execution(* SimpleService.getMessage(..)) to target the method.
3
Add @AfterReturning advice to log the returned value
In the LoggingAspect class, add an @AfterReturning advice method called afterReturningGetMessage() that runs after SimpleService.getMessage() returns. Capture the returned string in a parameter called result and log "Returned message: " followed by the result.
Spring Boot
Hint
Use @AfterReturning with pointcut and returning attributes to capture the returned value.
4
Complete the Spring Boot application setup
Add the @SpringBootApplication annotation to a class called Application with a main method that runs the Spring application using SpringApplication.run(Application.class, args).
Spring Boot
Hint
Use @SpringBootApplication and a main method to start the app.
Practice
(1/5)
1. What is the main difference between @After and @AfterReturning in Spring AOP?
easy
A. @After runs after method completion regardless of outcome; @AfterReturning runs only after successful return.
B. @After runs before the method; @AfterReturning runs after the method.
C. @After runs only if the method throws an exception; @AfterReturning runs always.
D. @After and @AfterReturning are exactly the same.
Solution
Step 1: Understand @After behavior
@After advice runs after a method finishes, whether it returns normally or throws an exception.
Step 2: Understand @AfterReturning behavior
@AfterReturning advice runs only if the method completes successfully without throwing exceptions.
Final Answer:
@After runs after method completion regardless of outcome; @AfterReturning runs only after successful return. -> Option A
Quick Check:
@After = always after, @AfterReturning = after success [OK]
Hint: Remember: @After always runs, @AfterReturning only on success [OK]
Common Mistakes:
Confusing @After with @Before advice
Thinking @AfterReturning runs on exceptions
Believing both advices behave identically
2. Which of the following is the correct syntax to define an @AfterReturning advice method in Spring AOP?
easy
A. @AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "result")
public void afterReturn(Object result) { }
B. @AfterReturning("execution(* com.example.*.*(..))")
public void afterReturn() { }
C. @AfterReturning(returning = "result")
public void afterReturn(Object result) { }
D. @AfterReturning(pointcut = "execution(* com.example.*.*(..))")
public void afterReturn(Object result) { }
Solution
Step 1: Check required attributes for @AfterReturning
The @AfterReturning annotation requires a pointcut expression and optionally a returning attribute to capture the returned value.
Step 2: Match method signature with returning attribute
The method must have a parameter matching the name in returning to receive the returned value.
Final Answer:
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "result")
public void afterReturn(Object result) { } -> Option A
Quick Check:
Use both pointcut and returning with matching parameter [OK]
Hint: Use both pointcut and returning with matching method parameter [OK]
Common Mistakes:
Omitting pointcut attribute
Not matching returning name with method parameter
Using @AfterReturning without parameters
3. Given the following Spring AOP advice, what will be printed if someMethod() throws an exception?
Hint: Match returning name with method parameter to avoid errors [OK]
Common Mistakes:
Omitting method parameter for returning value
Misnaming the parameter
Using @AfterReturning without pointcut
5. You want to log the returned value of a service method only when it completes successfully, and also log a message after the method finishes regardless of outcome. Which advice combination should you use?
hard
A. Use two @AfterReturning advices for both logs.
B. Use @AfterReturning to log the returned value and @After to log after method completion.
C. Use @After advice only for both logs.
D. Use @Before advice to log returned value and @AfterReturning to log after completion.
Solution
Step 1: Log returned value only on success
@AfterReturning advice runs only after successful method return, so it is suitable to log the returned value.
Step 2: Log after method finishes regardless of outcome
@After advice runs after method completion whether success or exception, so it is suitable to log a message always.
Final Answer:
Use @AfterReturning to log the returned value and @After to log after method completion. -> Option B
Quick Check:
AfterReturning = success only, After = always after [OK]
Hint: Combine @AfterReturning for success and @After for always [OK]
Common Mistakes:
Using @After for returned value logging
Using @Before to log returned value
Expecting @AfterReturning to run on exceptions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.