Understanding Spring AOP

Prasad Thilakarathne
3 min readJan 28, 2021

--

Spring AOP (Aspect Oriented Programming) is one of the key features of the Spring Framework. While Spring’s DI (Dependency Injection) decouples the application objects, Spring AOP decoupled application-wide concerns such as transactions, security, and logging from the objects in which they are applied.

What is Aspect-Oriented programming?

As stated earlier AOP decouples application-wide concerns or cross-cutting concerns. Cross-cutting concerns mean any functionality that affects multiple points of an application. Logging, Security, and Transaction management are examples of cross-cutting concerns.

Cross-cutting concerns — Understanding AOP
Cross-cutting concerns — Understanding AOP
AOP Increase modularity by separation of cross-cutting concerns from business logic — Understanding AOP — Prasadct.com
AOP Increase modularity by separation of cross-cutting concerns from business logic — Understanding AOP

In order to understand further, you have to familiarize yourself with some jargon related to AOP.

Aspect

Aspect is the cross-cutting concerns that we need to modularize. For example, logging, transaction management, and security are aspects.

Advice

Advice is the concrete implementation of Aspect. It defines what is the job of aspect and when to perform it.

Types of advice

  • Before — Advice functionality executes before the advised method is invoked.
  • After — Advice functionality executes after the advised method is invoked.
  • After throwing — Advice functionality executes after the advised method throws an exception.
  • After returning — Advice functionality executes after method return successful result without an exception.
  • Around — Advice functionality executes before and after the advised method is invoked.

NOTE: Spring supports only method level aspects. We only can use the above Advice types for the methods. Before, After, Around, or After throwing an exception from a method.

Joint points

Join points are the places where your aspect functionality can be plug into the application flow. This is usually a method being called or an exception being thrown.

Pointcuts

Pointcuts define on which joint points the aspect needs to apply. This is decided by the pointcut expression associated with advice which is usually a method name or regular expression

In Spring all the Advice is written in Java but the pointcuts and joint points can be defined using XML or Annotations.

Spring AOP Example with Annotations

How Spring aspects internally work?

In Spring, aspects are implemented as proxies that wrap the targeted method. Since spring creates proxies at runtime, proxies objects will be created when it loads from BeanFactory.

Understanding Spring AOP

It is really important to understand the above basic concepts of Spring AOP before going to implement them in your own projects. I didn’t include XML based AOP example because the annotation-based configuration is simple and there is no need to go for XML-based configurations with these new Spring-boot features.

--

--