In this article, we will learn Aspect oriented Concepts with spring
Aspect Oriented Programming
AOP is not specific to Spring Framework, rather its general concept widely used in various frameworks. So why AOP is adopted in Spring Framework, when its rich core concept is built around Dependency Injection?
There are various concerns which cut-across multiple classes/types and are scattered across multiple classes throughout the code in an enterprise application. These crosscutting concerns are
- Logging
- Transaction Management
- Security Management
- Profiling
To remove all these crosscutting concerns codes scattered across all modules and bring it in one common place thereby achieving modularity. So this way, crosscutting concerns are modularized removing the code from the application and it is implemented using aspects
AOP Terminologies
Aspect: modularization of concerns that are cut across through multiple classes/types
Join Point: set of possible points in the execution of the program such as method execution, exception handling. In Spring AOP, a join point always represents a method execution
Advice: actual advice taken at the join point during program execution
Pointcut: predicates that matches the join point. Poincut expression determines where the actual advice needs to be applied during runtime
Target Object: object being advised by one/more aspects at runtime. But these objects are always proxied object as Spring AOP is implemented using runtime proxies
AOP Proxy: object created by aspects at runtime. In Spring AOP, AOP proxy is created using JDK dynamic proxy or CGLIB proxy
Weaving: linking aspect with object to create advised object
For details on Spring AOP see here
Advice in Spring
We can implement advices in spring using two approaches
- Having advice class which implements respective interfaces for the concenrs
- AspectJ based annotations covering crosscutting concerns
Note: we will implement each advice using both approaches in the following articles
Types of advices:
- Before advice: executes before method execution
Interface: org.springframework.aop.BeforeAdvice
Annotation:@Before (org.aspectj.lang.annotation.Before) - After returning advice: executes after method execution completes normally
Interface: org.springframework.aop.AfterReturningAdvice
Annotation:@AfterReturning (org.aspectj.lang.annotation.AfterReturning) - After (finally) advice: executes after method execution irrespective of the outcome
Interface: org.springframework.aop.AfterAdvice
Annotation:@After(org.aspectj.lang.annotation.After) - After throwing advice: executes after method throws an exception
Interface: org.springframework.aop.ThrowsAdvice
Annotation: @AfterThrowing(org.aspectj.lang.annotation.AfterThrowing) - Around advice: executes before & after method execution, but requires proceed() method to continue the execution
Interface: org.aopalliance.intercept.MethodInterceptor
Annotation:@Around(org.aspectj.lang.annotation.Around)
In the coming discussions, we will extend the above article to see the detailed examples for each advice in separate articles covering both approaches
Read Also:
- Spring AOP: Before Advice
- Spring AOP: After Returning Advice
- Spring AOP: After (Finally) Advice
- Spring AOP: After Throwing Advice
- Spring AOP: Around Advice
Happy Coding !!
Happy Learning !!