Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring : Difference between @ControllerAdvice and Spring AOP

The @ControllerAdvice annotation allows us to write global code applicable to a wide range of controllers.

My Question is:

@ControllerAdvice is applicable to only controller level and Spring AOP is based on pointcut.

Other than this, what are the differences between them and when to use @ControllerAdviceand Spring AOP?

I was trying to implement something like shown in this example link.

Link Here

like image 787
cpatel Avatar asked Aug 01 '26 09:08

cpatel


1 Answers

@ControllerAdvice is one of the AOP features Spring offers. The main difference in use case is that @ControllerAdvice is wired up by the Spring MVC infrastructure and uses (and provides) Web-specific features. Use it if you're writing advice that specifically applies to Web requests, such as error handling (e.g., translating exceptions into an enterprise-standard JSON error format). Otherwise, aspects are the approach.

like image 161
chrylis -cautiouslyoptimistic- Avatar answered Aug 02 '26 22:08

chrylis -cautiouslyoptimistic-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!