cpatel
cpatel

Reputation: 231

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

Upvotes: 7

Views: 5848

Answers (2)

Anurag
Anurag

Reputation: 21

The main difference can be understood in terms of Scope and Use-case. ControllerAdvice is specialized for Controller, while SpringAOP is general purpose. Both are designed for cross cutting concerns, but they serve different purposes. ControllerAdvice is integrated with SpringMVC and is used in Exception Handling, it is not like traditional AOP.

Upvotes: 0

@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.

Upvotes: 8

Related Questions