![]() |
VOOZH | about |
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.
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.
The architecture of Spring AOP Advice is as follows:
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.
This advice runs before the actual business method execution.
This class contains the actual method where logic is executed.
In this step, we will create an Advisor class and name it as BeforeAdvisor, and do remember this class will implement the interface MethodBeforeAdvice.
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.
Here, we will add the required dependencies in our pom.xml file.
File: pom.xml
In this step, we will create a Test.java class and initialize the beans and calls message() method of Geeks.java class.
Test.java
we will run our application and get the output.
Executes after method execution completes successfully.
We will be using the same Geeks.java class for actual business logic.
In this step, we will create a class and name it AfterAdvisor.java and implement the AfterReturningAdvice interface and implements its method afterReturning().
Here we will be updating our application-context.xml file according to the AfterAdvisor class and AfterReturningAdvice interface.
We will run our application.
Executes when an exception is thrown.
Here we will be updating our business logic class Geeks.java. Below is the code for Geeks.java 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.
In this step, we will update our application-context.xml file according to ThrowsAdvisor.
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.
It is as Implementation below step by step as shown below as follows:
In this step, we will update our business logic class Geeks.java. Below is the code for Geeks.java 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.
Now, we will change our application-context.xml file according to AroundAdvice.
application-context.xml
In this step, we will use our previous 'Test.java' and update it according to the business requirement.
Test.java