@After Advice in Spring: What It Is and How It Works
@After advice in Spring is a type of aspect that runs a piece of code after a method finishes, regardless of its outcome. It is used to execute logic once a method completes, whether it returns normally or throws an exception.How It Works
Imagine you have a friend who always checks in with you after you finish a task, no matter if you succeeded or failed. @After advice works similarly in Spring. It lets you run some code right after a method finishes, whether the method ended well or with an error.
This is part of Aspect-Oriented Programming (AOP) in Spring, where you can add extra behavior around your main code without changing it. The @After advice attaches to a method and triggers after the method's execution is complete, making it useful for cleanup or logging tasks.
Example
This example shows a simple Spring aspect using @After advice that runs after a method named performTask finishes.
package com.example.aspect; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @After("execution(* com.example.service.MyService.performTask(..))") public void afterPerformTask() { System.out.println("Method performTask has finished."); } } // Service class package com.example.service; import org.springframework.stereotype.Service; @Service public class MyService { public void performTask() { System.out.println("Performing task..."); // Task logic here } } // Main Application package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import com.example.service.MyService; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(DemoApplication.class, args); MyService service = context.getBean(MyService.class); service.performTask(); } }
When to Use
Use @After advice when you want to run code after a method completes no matter what happened inside it. This is helpful for:
- Cleaning up resources like closing files or database connections.
- Logging that a method finished, regardless of success or failure.
- Triggering follow-up actions that must happen after the main work.
For example, if you want to log every time a service method ends or ensure some cleanup always runs, @After advice is a good choice.
Key Points
- @After advice runs after the method finishes, whether it returns normally or throws an exception.
- It is part of Spring AOP and helps separate cross-cutting concerns like logging and cleanup.
- Use
@Afterwhen you want guaranteed execution after a method ends. - It differs from
@AfterReturning(runs only on success) and@AfterThrowing(runs only on exceptions).
Key Takeaways
@After advice runs code after a method finishes regardless of outcome.@After when you need guaranteed execution after method completion.