Spring AOP around advice Example

By | June 25, 2021

In this post, We will learn about the Spring AOP around advice for Example using a Demo Project.

One of the very important key units of Spring Framework is the Aspect-oriented programming (AOP) framework. Aspect-Oriented Programming helps us to provide functionalities to break down program logic into distinct parts or modules is called AOP concerns. The functions that spread across multiple sections of an application are called cross-cutting concerns and these cross-cutting concerns functionalities must be separate from the application’s business logic. There are various good examples of aspects like logging, authentication, transaction management, auditing, spring-security, caching, etc.

The key concept of modularity is Object-Oriented Programming is the class, whereas in AOP the unit of modularity is the aspect. Spring dependency Injection usually helps us to lose coupling our application objects from each other and AOP helps us to decouple cross-cutting concerns from the objects that they affect. 

Spring AOP provides interceptors to intercept an application. For example, when a method is executed, We can add extra functionality before or after the method execution.

Types of Advice

Spring AOP has five kinds of advice mentioned as Below −

1 before

This advice executes before a join point, but this advice does not have the ability to prevent execution flow to proceed to the join point (unless it throws an exception).

2 after

This advice is executed after a join point completes normally: for example if a method returns without throwing an exception.

3 after-returning

This advice runs after the method execution only if the method completes successfully.

4 after-throwing

This advice is executed if a method exiting by throwing an exception.

5 around

This is the most powerful kind of advice We can use around advice to perform custom behavior before and after the method invocation. This advice is also responsible to select whether to proceed to the joinpoint or to perform the advised method execution by returning its own return value or throwing an exception.

In this demo project, We will learn about Around Advice using XML Config:


We need to add spring and spring-aspects dependencies in Maven pom.xml file


The model class which hold Account information data


AccountService interface


AccountService interface implementation class


Custom Exception is designed to throw when Account details not found in the repository


This is the Simple java class where we have defined a method aroundAdvice which accepts ProceedingJoinPoint as an argument and this method we have configured as around advice in the applicationContext.xml spring config file.



To use the AOP namespace tags we need to import the spring AOP schema as shown in the below config file

An aspect is declared using the <aop:aspect> element within <aop:config> tag and the backing bean is referenced using the ref attribute in <aop:aspect>

Here “accountServiceAspect” is configured and dependency injected just like any other Spring bean.

pointcut helps us to determine the join points (ie methods) of interest to be executed with different advice. Pointcut is defined here using <aop:pointcut> tag within <aop:config>

In this below config, we have defined a pointcut named ‘selectAll’ that will match the execution of updateAccountBalance(Account account,Long amount) method available in the AccountServiceImpl class under the package com.kkhindigyan.org.service.impl

We can usually declare any of the five advices inside an <aop:aspect> using the <aop:{Advice Name}> element


This ClientTest class gets the AccountService bean from the applicationContext.xml file and calls the updateAccountBalance method of the EmployeeService after that around advice will be called automatically. 

If you run ClientTest.java as Java Application then it will give the below output:

That’s all about Spring AOP around advice Example

You May Also Like:

Spring AOP Concepts and Terminology
Spring AOP Before and After Advice Example
Spring AOP Before and After Advice Using Annotations Example
Spring AOP AfterReturning and AfterThrowing Advice Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
Spring 5 and Hibernate 5 integration CRUD Example
Spring HibernateTempate Example
Spring and Hibernate Declarative Transaction Management Example

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

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