Course introduction:

Spring AOP makes Aspect-Oriented Programming easier. In this tutorial, you will learn about aspect-oriented programming using Spring AOP. Aspect-oriented programming enhances the Object-Oriented Programming concept by providing a different way to structure your code. Spring AOP uses AspectJ internally, we will use Spring with AspectJ annotations in this tutorial.

What is AOP?

An Aspect is simply a common feature present across the code (classes, methods, etc). It is the repeated code or logic which you think can have a better structure to manage instead of scattering it across different classes and methods.

For instance, code to trace the performance matrices (Profiling), Logging codes, Database transaction management code, etc are good examples of aspects. Features like this are called crosscutting concerns.

The Aspect-Oriented Programming (AOP) recommends you to abstract and encapsulate the crosscutting concerns into separate code.

What is Spring AOP?

Spring AOP is one of the important components of Spring and it complements Spring IoC to provide a very capable middleware solution. Spring AOP uses AspectJ internally, you can also extend the AOP API.

AOP is used in the Spring Framework to:

  1. Provide declarative enterprise services – e.g. transaction management.
  2. Let users implement custom aspects, complementing their use of OOP with AOP.

Spring AOP Terminology

The AOP terminologies are a bit confusing, but let us learn them. Spring does not add any own terminology, these are pure AOP concepts.

  • Aspect – It is the crosscutting concern that is written/refactored into a separate class and annotated with @Aspect.
  • Join Point – In Spring AOP, a Join Point always represents a method execution. It is the point where an Aspect will be applied.
  • Advice – It is an action taken by an aspect at a particular join point. Spring models an advice as an interceptor and maintains a chain of interceptors around the join point. There are different types of advices included in Spring.
    • Before advice – @Before
    • Around advice – @Around
    • After returning – @AfterReturning
    • After throwing – @AfterThrowing
    • After (finally) advice – @After
  • Pointcut – It is an expression that matches zero or more Join points. Advice associates with a pointcut expression and runs at any join point that matches with the Pointcut.
  • Target Object – Objects on which advices are applied. Spring AOP uses runtime proxies so this object is always a proxy object.
  • AOP Proxy – An object created by the AOP framework to implement the aspects. It created with JDK dynamic proxy or CGLIB proxy.
  • Weaving – It links aspects with other 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.
  • Introduction – Spring AOP lets you introduce new interfaces (and a corresponding implementation) to any advised object. An introduction is known as an inter-type declaration.

Download course content


You should read this only after completing the entire Tutorial and after knowing the pros and cons of Aspect-Oriented Programming. AOP is a great addition to Spring, especially the way Spring Security offers us amazing features with simplicity. There are some issues that need to be addressed and problems to be solved. One has to use proper guidelines to implement AOP in bigger projects, otherwise debugging could become painful. Furthermore, modelling software applications that use AOP elements becomes difficult to represent in UML. If you are thinking of next-generation java programming e.g. GraalVM native build, AOP is not a good idea.

By |Last Updated: May 2nd, 2020|Categories: Spring Framework|