Spring AOP allows adding additional behavior to the existing code without its modification. The Spring 1.2 Old Style AOP is supported in Spring 3 but there are a few things such as advice domain objects, that can not be done easily with Spring AOP. The AspectJ is the best available option in that case. And so, it is recommended to use Spring AOP with AspectJ.
The most important module of Aspect-Oriented Programming is advice. Advice is an action performed by an aspect at a particular join-points. There are 4 types of advices in Spring 1.2 Old Style AOP:
- Before Advice: It is executed before the actual method call.
- After Advice: It is executed after the actual method call.
- Around Advice: It is executed before and after the actual method call.
- Throws Advice: It is executed if the actual method throws an exception.
The architecture of Spring AOP Advice is as follows:

Architecture Of Spring AOP
A. MethodBeforeAdvice
It is shown step by step that is as follows:
Step 1: Create a business logic class
In this step, we will create a class that will contain actual business logic. For this, we will create a new class and name it Geeks. And add the following line of codes to it.
// Class
public class Geeks {
// for actual business logic class we will
// define a public method to print some random text
public void message() {
System.out.println("Welcome to GeeksforGeeks!!");
}
}
Step 2: Creating Advisor class
In this step, we will create an Advisor class and name it as BeforeAdvisor, and do remember this class will implement the interface MethodBeforeAdvice.
Example:
Java
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvisor implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1,
Object arg2) throws Throwable
{
System.out.println(
"This is called before the actual call.." );
}
}
|
Before moving further we need to know about ProxyFactoryBean. It is a class provided by the Spring framework. ProxyFactoryBean class contains 2 fields target and interceptorName. For our example, the instance of Geeks class will be considered as target and the instance of advisor class as an interceptor.
Step 3: Create an application-context.xml file
In this step, we will create beans in the context file for our Geeks, Advisor, and ProxyFactoryBean class.
Note: You need to provide your class path and the package definition while creating beans.
File: application-context.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "geeksObj" class = "com.geeksforgeeks.Geeks" ></ bean >
< bean id = "beforeAdvisorObj" class = "com.geeksforgeeks.BeforeAdvisor" ></ bean >
< bean id = "proxyObj" class = "org.springframework.aop.framework.ProxyFactoryBean" >
< property name = "target" ref = "geeksObj" ></ property >
< property name = "interceptorNames" >
< list >
< value >beforeAdvisorObj</ value >
</ list >
</ property >
</ bean >
</ beans >
|
Step 4: Adding dependencies
Here, we will add the required dependencies in our pom.xml file.
File: pom.xml
XML
< modelVersion >4.0.0</ modelVersion >
< groupId >com.geeksforgeeks</ groupId >
< artifactId >SpringAOP</ artifactId >
< version >0.0.1-SNAPSHOT</ version >
< dependencies >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-context</ artifactId >
< version >5.1.3.RELEASE</ version >
</ dependency >
< dependency >
< groupId >aspectj</ groupId >
< artifactId >aspectjrt</ artifactId >
< version >1.5.4</ version >
</ dependency >
< dependency >
< groupId >org.aspectj</ groupId >
< artifactId >aspectjweaver</ artifactId >
< version >1.9.2</ version >
</ dependency >
< dependency >
< groupId >org.aspectj</ groupId >
< artifactId >aspectjtools</ artifactId >
< version >1.9.2</ version >
</ dependency >
< dependency >
< groupId >org.ow2.asm</ groupId >
< artifactId >asm</ artifactId >
< version >7.0</ version >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-aspects</ artifactId >
< version >5.1.3.RELEASE</ version >
</ dependency >
</ dependencies >
</ project >
|
Step 5: Creating application/main file
In this step, we will create a Test.java class and initialize the beans and calls message() method of Geeks.java class.
File: Test.java (Main File/Application File)
Java
import java.util.ResourceBundle;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args)
{
Resource context = new ClassPathResource(
"application-context.xml" );
BeanFactory beanFactory
= new XmlBeanFactory(context);
Geeks geeks
= beanFactory.getBean( "proxyObj" , Geeks. class );
geeks.message();
}
}
|
Step 6: Appending output via running application file.
In this step, we will run our application and get the output.

Fig 2 – Output
B. AfterReturningAdvice
Implementation:
Step 1: Create a business logic class
We will be using the same Geeks.java class for actual business logic.
Step 2: Create AfterAdvisor class
In this step, we will create a class and name it AfterAdvisor.java and implement the AfterReturningAdvice interface and implements its method afterReturning().
// Java Program to Illustrate AfterAdvisor Class
// Importing required classes
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
// Class
public class AfterAdvisor
implements AfterReturningAdvice
{
// Method
// Executes after the actual business logic call
public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
throws Throwable
{
System.out.println("This is called after returning advice..");
}
}
Step 3: Changing the application-context.xml file
Here we will be updating our application-context.xml file according to the AfterAdvisor class and AfterReturningAdvice interface.
Example
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "geeksObj" class = "com.geeksforgeeks.Geeks" ></ bean >
< bean id = "afterAdvisorObj" class = "com.geeksforgeeks.AfterAdvisor" ></ bean >
< bean id = "proxyObj" class = "org.springframework.aop.framework.ProxyFactoryBean" >
< property name = "target" ref = "geeksObj" ></ property >
< property name = "interceptorNames" >
< list >
< value >afterAdvisorObj</ value >
</ list >
</ property >
</ bean >
</ beans >
|
Step 4: Output
In this step, we will run our application.

Fig 3 – Output
C. ThrowsAdvice
Procedure: It is as Implementation below step by step as shown below as follows:
Step 1: Create a business logic class
Here we will be updating our business logic class Geeks.java. Below is the code for Geeks.java class.
// Class
public class Geeks {
// For actual business logic class we will
// define a public method to print some random text
public void message(String str) {
// Checking if string is equal or not
if (str.equals("geeksforgeeks.org"))
// Print statement as string is equal
System.out.println("Welcome to GeeksforGeeks!!");
else
// Do throw exception
throw new IllegalArgumentException("Invalid String");
}
}
Step 2: Create ThrowsAdvisor class
In this step, we will create a class and name it ‘ThrowsAdvisor.java’ and we will implement the ThrowsAdvice interface and implement the afterThrowing() method. Here, we will put those concerns which will need to be thrown even if the exception has occurred.
Example
Java
import org.springframework.aop.ThrowsAdvice;
public class ThrowsAdvisor implements ThrowsAdvice {
public void afterThrowing(Exception e)
{
System.out.println(
"This will be called if exception occurs.." );
}
}
|
Step 3: Changing the application-context.xml file
In this step, we will update our application-context.xml file according to ThrowsAdvisor.
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "geeksObj" class = "com.geeksforgeeks.Geeks" ></ bean >
< bean id = "throwsAdvisorObj" class = "com.geeksforgeeks.ThrowsAdvisor" ></ bean >
< bean id = "proxyObj" class = "org.springframework.aop.framework.ProxyFactoryBean" >
< property name = "target" ref = "geeksObj" ></ property >
< property name = "interceptorNames" >
< list >
< value >throwsAdvisorObj</ value >
</ list >
</ property >
</ bean >
</ beans >
|
Step 4: Creating Test.java class
In this step, we will create a new class and name ‘Test.java’. In this class, we will initialize our beans and call message(String str) method of our business logic class Geeks.java. We will pass an invalid string so, our application will throw an exception and still call ThrowsAdvice.
File: Test.java (Application Class)
Java
import java.util.ResourceBundle;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args)
{
Resource context = new ClassPathResource(
"application-context.xml" );
BeanFactory beanFactory
= new XmlBeanFactory(context);
Geeks geeks
= beanFactory.getBean( "proxyObj" , Geeks. class );
try {
geeks.message( "gfg" );
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Step 5: Run the application.
Output:

Fig 4 – Output
D. MethodInterceptor(AroundAdvice)
Procedure:
It is as Implementation below step by step as shown below as follows:
Step 1: Creating a business logic class
In this step, we will update our business logic class Geeks.java. Below is the code for Geeks.java class.
Example
// Class
public class Geeks {
// Method
// Define a public method to print some random text
// for actual business logic class we will
public void message()
{
// Print statement
System.out.println("This is around advice..");
}
}
Step 2: Create AroundAdvice class
In this step, we will create a new class and name as AroundAdvice and with this class, we will implement the ‘MethodInterceptor interface‘ and provide the definition for invoke() method.
Example
Java
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation invocation)
throws Throwable
{
Object obj;
System.out.println(
"This will be run before actual business logic..." );
obj = invocation.proceed();
System.out.println(
"This will be run after actual business logic..." );
return obj;
}
}
|
Step 3: Update the application-context.xml file
Now, we will change our application-context.xml file according to AroundAdvice.
File: application-context.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "geeksObj" class = "com.geeksforgeeks.Geeks" ></ bean >
< bean id = "aroundAdvisorObj" class = "com.geeksforgeeks.AroundAdvice" ></ bean >
< bean id = "proxyObj" class = "org.springframework.aop.framework.ProxyFactoryBean" >
< property name = "target" ref = "geeksObj" ></ property >
< property name = "interceptorNames" >
< list >
< value >aroundAdvisorObj</ value >
</ list >
</ property >
</ bean >
</ beans >
|
Step 4: Create Test.java file
In this step, we will use our previous ‘Test.java’ and update it according to the business requirement.
FIle: Test.java
Java
import java.util.ResourceBundle;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args)
{
Resource context = new ClassPathResource(
"application-context.xml" );
BeanFactory beanFactory
= new XmlBeanFactory(context);
Geeks geeks
= beanFactory.getBean( "proxyObj" , Geeks. class );
geeks.message();
}
}
|
Step 5: Output
In this step, we will run our application.

Fig 5 – Output
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
06 Feb, 2023
Like Article
Save Article