VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-aop-example-spring1-2-old-style-aop/

⇱ Spring - AOP Example (Spring1.2 Old Style AOP) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring - AOP Example (Spring1.2 Old Style AOP)

Last Updated : 2 May, 2026

Spring AOP allows you to add extra functionality (like logging, security, etc.) to existing code without modifying it. The older Spring 1.2-style AOP is supported but has limitations, so using AspectJ with Spring AOP is recommended for better flexibility.

  • Adds behavior without changing existing code
  • Helps separate cross-cutting concerns
  • AspectJ provides more powerful AOP features

Advice in AOP

Advice is the action performed by an aspect at a specific join point in the program. It defines when and how additional behavior is applied to a method.

  • Before Advice: Runs before the method execution
  • After Advice: Runs after the method execution
  • Around Advice: Runs before and after the method
  • Throws Advice: Runs when an exception occurs

The architecture of Spring AOP Advice is as follows:

👁 Image
Architecture Of Spring AOP

Spring AOP Advice Types – Step-by-Step Implementation

In Spring AOP, Advice defines what action should be taken and when it should be executed. Below are the main types of advice with clear step-by-step implementation.

1. MethodBeforeAdvice (Before Advice)

This advice runs before the actual business method execution.

Step 1: Create Business Logic Class

This class contains the actual method where logic is executed.

Step 2: Create 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.

Step 3: Configure application-context.xml

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

Step 4: Adding dependencies

Here, we will add the required dependencies in our pom.xml file.

File: pom.xml

Step 5: Create Main Class

In this step, we will create a Test.java class and initialize the beans and calls message() method of Geeks.java class.

Test.java

Output

we will run our application and get the output.

👁 Image
Fig 2 - Output

2. AfterReturningAdvice

Executes after method execution completes successfully.

Step 1: Create a business logic class

We will be using the same Geeks.java class for actual business logic.

Step 2: Create After Advisor

In this step, we will create a class and name it AfterAdvisor.java and implement the AfterReturningAdvice interface and implements its method afterReturning().

Step 3: Update XML Configuration

Here we will be updating our application-context.xml file according to the AfterAdvisor class and AfterReturningAdvice interface.

Output

We will run our application.

👁 Image
Fig 3 - Output

3. ThrowsAdvice

Executes when an exception is thrown.

Step 1: Modify Business Class

Here we will be updating our business logic class Geeks.java. Below is the code for Geeks.java class.

Step 2: Create Throws Advisor

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. 

Step 3: Changing the application-context.xml file

In this step, we will update our application-context.xml file according to ThrowsAdvisor.

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

Test.java

Step 5: Run the application.

Output:

👁 Image
Fig 4 - Output

4. MethodInterceptor(AroundAdvice)

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.

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.

Step 3: Update the application-context.xml file

Now, we will change our application-context.xml file according to AroundAdvice.

application-context.xml

Step 4: Create Test.java file

In this step, we will use our previous 'Test.java' and update it according to the business requirement.

Test.java

Output

In this step, we will run our application.

👁 Image
Fig 5 - Output
Comment
Article Tags:
Article Tags: