Learn Spring AOP in 30 minutes

Learn Spring AOP

Learn Spring AOP in 30 minutes


Aspect Oriented Programming (AOP) is another approach to programming like Object Oriented Programming (OOP). However, AOP can complement OOP by providing another way to think about the program structure with which a software application is developed. There are certain similarities in how OOP and AOP are used to build software code although they are named differently – in OOP the unit of modularity is known as class while that in AOP is known as Aspect. AOP applications are developed based on the Spring framework though the Spring framework can also be used without the AOP. This article discusses how to work with Spring AOP applications.

What is AOP?

AOP, as the name suggests, is another approach to software programming. Let us understand AOP with the help of an example. A program structure for a software application contains three layers: UI layer, business logic layer and database layer. To function properly, all layers need support and communication. Support and communication can be provided by efficient logging, security, transaction management, profiling and more. In AOP parlance, the various units that provide support are known as concerns. Since these concerns provide support across layers, they are also referred to as cross-cutting concerns. These cross-cutting concerns are encapsulated in what are known as Aspects. For example, a logging module can be called an AOP Aspect for logging. Aspects are user-defined and can be defined on run time basis. There are many advantages of using Aspects. One, they are reusable across software applications and enterprises; two, since they contain procedures or functions, software developers just need to use them in their program structure; three, Aspects reduce development overhead.

Must read – Learn Spring Boot in 30 Minutes 

Must read – Steps to learn Spring Batch Service 

AOP Concepts

AOP concepts drive the AOP structure and to use AOP, it is required to learn the concepts. The main concepts are explained below.

Concept name Description
Aspect Aspect is a module that comprises a set of APIs providing one or more cross-cutting concerns. For example, if the security needs to be implemented in a software application, the security Aspect can provide security concerns across application layers.
Join Point This is the place in the software application where an action or step will be taken using the Spring AOP framework.
Advice This is the action to be executed before or after the method execution. Actually, it is the piece of code invoked during the program execution. There are five types of advices: before, after, after-returning, after-throwing and around. For example, <!– a before advice definition –>

<aop:before pointcut-ref=”newbusinessService”

method=”doRequiredTask”/> means that the advice before will be run before the code is executed.

Pointcut Place in the code where an advice should be executed.
Introduction Allows you to add classes or methods to existing classes.
Target Object This is the object that is advised by one or more aspects.
Weaving It is the process of linking aspects with application objects or types to create an advised object. This can be done at run time, loading time or at the time of compilation.

Building an application with AOP

Now that we have some knowledge of the basics of AOP, we will see below how the concepts can be used to build a code with one or more examples:

Declare an aspect –

The example below shows how an aspect can be declared:


<aop:aspect id=”aopAspect” ref=”aopBean”>



<bean id=”aopBean” class=”…”>


Declare a pointcut –

The example of pointcut is given below:


<aop:aspect id=”aopAspect” ref=”aopBean”>

<aop:pointcut id=”aopbusinessService”

expression=”execution(* com.techalpine.Emp.getSal(..))”/>



<bean id=”yBean” class=”…”>


In the above declaration, a pointcut named aopbusinessService is declared and it will match the execution of getSal() method.

Declare Advice –

The example below shows how all five advices can declared:


<aop:aspect id=”aopAspect” ref=”aopBean”>

<aop:pointcut id=”aopbusinessService”

expression=”execution(* com.techalpine.aopapp.service.*.*(..))”/>

<!– a before advice definition –>

<aop:before pointcut-ref=”aopbusinessService”


<!– an after advice definition –>

<aop:after pointcut-ref=”aopbusinessService”


<!– an after-returning advice definition –>

<!–The doAOPTask method must have parameter named aopVal –>

<aop:after-returning pointcut-ref=”aopbusinessService”



<!– an after-throwing advice definition –>

<!–The doAOPTask method must have parameter named aopex –>

<aop:after-throwing pointcut-ref=”aopbusinessService”



<!– an around advice definition –>

<aop:around pointcut-ref=”aopbusinessService”

method=”doAOPTask”/>   …



<bean id=”yBean” class=”…”>…


As can be seen from the above example, all five advices were declared using the <aop:aspect> element.

When this code is executed, it will display the point cuts and the output at those points. The output will depend on the point cut definitions and the advice types.

You will also like to read – Spring with Testing frameworks – How does it work?

Spring video tutorials – Watch and learn 


While AOP seems to be a pretty good option because of the re-usability aspect of its concerns, it is not without its problems. One, it can be an issue debugging an AOP framework-based code because the business classes are advised only after the aspects are configured. Two, Advise can be given only for public methods and not for methods with default, private or public visibility. Still, AOP can be a good option because it complements OOP so well.

Spring Archive – Explore more interesting articles on Spring framework 

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

− 4 = 5

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email