Spring AOP

Bushra Saifi
4 min readMay 2, 2019

--

AOP stands for Aspect oriented programming. It is a design principal of Spring Framework.

Dependency injection helps us to decouple application objects while AOP helps us to decouple cross cutting concerns from the object.The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

A cross-cutting concern is a concern that can affect the whole application and should be centralized in one location in code as possible, such as transaction management, authentication, logging, security etc.

Main java based AOP implementations are listed below :

  1. AspectJ
  2. Spring AOP
  3. JBoss AOPs

AOP concepts and terminologies:-

Aspect:- It is a class that contains advices, joinpoints etc. or we can say it is a class that provide cross cutting concerns .For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.

@Aspect
public class LoggingAspect {

@Before("execution(public String get*())")
public void loggingAdvice()
{
System.out.println("advice run.. getName method called");
}
}

https://stackoverflow.com/questions/15447397/spring-aop-whats-the-difference-between-joinpoint-and-pointcut

JoinPoints:- These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.This point could be a method being called, an exception being thrown, or even a field being modified.

JoinPoint instance has information about actual method called that triggered advice. if getter is called then Joinpoint have info about getter.

@Before("execution(public String getName()")
public void loggingAdvice(JoinPoint joinPoint) {
System.out.println("advice run.. getName method called");
//System.out.println(joinPoint.toString());
Circle circle = (Circle) joinPoint.getTarget();

}

Pointcut:- A pointcut defines at what joinpoints, the associated Advice should be applied. Advice can be applied at any joinpoint supported by the AOP framework.we can specify PointCuts using expressions or patterns.

@Aspect
public class LoggingAspect {

@Before("allGetters()")
public void loggingAdvice()
{
System.out.println("advice run.. getName method called");
}

@Pointcut("execution(public String get*())")
public void allGetters() {}
}
  • To apply an advice on all the methods of a class we write a pointcut as —

@Pointcut(“execution(* * org.example.Circle.*(..)”) , .. means zero or more

Another way to do this is —

@Pointcut(“within(classname)”)

  • to apply an advice on all the methods of all the class inside a package

@Pointcut(“within(package name..*)”)

combining pointcuts:-

@Before("allGetters() && allCircleMethods()")
public void loggingAdvice() {
System.out.println("advice run.. getName method called");
}

Advice:- This is the actual action to be taken either before or after the method execution. This is the actual piece of code that is invoked during program execution by Spring AOP framework.

An advice can executeon the call we make in the program. it does not execute for the call that Spring container

Advice types: —

  1. before — Run advice before the method execution.
@Before("execution(public String get*())")
public void loggingAdvice()
{
System.out.println("advice run.. getName method called");
}

2. after — Run advice after the method execution, regardless of its outcome.

@After("execution(public String get*())")
public void SeconAdvice()
{
System.out.println("second advice run.. after method execution");
}

3. after-returning — Run advice after the method execution, only if the method completes successfully.

@AfterReturning(pointcut = "args(name)" , returning = "returnString")
public void stringArgument(String name, String returnString) {
System.out.println("A method that takes string arg has been called,the value is:" + name+"and output is:"+ returnString);
}

4. after-throwing — Run advice after the method execution, only if the method exits by throwing an exception.

@AfterThrowing(pointcut="args(name)", throwing = "ex")
public void exceptionAdvice(String name, RuntimeException ex) {
System.out.println("run even an exception will occur and exception is:"+ ex);
}

5. around — Run advice before and after the advised method is invoked.

@Around("execution( public * get*())")
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint)
{

try {
System.out.println("before advice");
proceedingJoinPoint.proceed();
System.out.println("after advice");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}

Proxy: — a proxy is an object that looks like another object, but adds special functionality behind the scene.

Spring AOP is proxy-based. AOP proxy is an object created by the AOP framework in order to implement the aspect contracts in runtime.

AspectJProxyFactory − Factory class to create a proxy object.

Weaving: — Weaving is the process of linking aspects with other outsider application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime only. In contrast, the AspectJ framework supports both compile-time and load-time weaving.

Target object : — The target object is an object being advised by one or more aspects. It will always be a proxy object. It is also referred to as the advised object.

--

--