Spring AOP Example
There are given examples of Spring1.2 old style AOP (dtd based) implementation.
Though it is supported in spring 3, but it is recommended to use spring aop with aspectJ that we are going to learn in next page.
There are 4 types of advices supported in spring1.2 old style aop implementation.
- Before Advice it is executed before the actual method call.
- After Advice it is executed after the actual method call. If method returns a value, it is executed after returning value.
- Around Advice it is executed before and after the actual method call.
- Throws Advice it is executed if actual method throws exception.
To understand the basic concepts of Spring AOP, visit the previous page.
Understanding the hierarchy of advice interfaces
Let’s understand the advice hierarchy by the diagram given below:
All are interfaces in aop.
MethodBeforeAdvice interface extends the BeforeAdvice interface.
AfterReturningAdvice interface extends the AfterAdvice interface.
ThrowsAdvice interface extends the AfterAdvice interface.
MethodInterceptor interface extends the Interceptor interface. It is used in around advice.
1) MethodBeforeAdvice Example
Create a class that contains actual business logic.
File: A.java
Now, create the advisor class that implements MethodBeforeAdvice interface.
File: BeforeAdvisor.java
In xml file, create 3 beans, one for A class, second for Advisor class and third for ProxyFactoryBean class.
File: applicationContext.xml
Understanding ProxyFactoryBean class:
The ProxyFactoryBean class is provided by Spring Famework. It contains 2 properties target and interceptorNames. The instance of A class will be considered as target object and the instance of advisor class as interceptor. You need to pass the advisor object as the list object as in the xml file given above.
The ProxyFactoryBean class is written something like this:
Now, let’s call the actual method.
File: Test.java
Output
Printing additional information in MethodBeforeAdvice
We can print additional information like method name, method argument, target object, target object class name, proxy class etc.
You need to change only two classes BeforeAdvisor.java and Test.java.
File: BeforeAdvisor.java
File: Test.java
Output
2) AfterReturningAdvice Example
Create a class that contains actual business logic.
File: A.java
Same as in the previous example.
Now, create the advisor class that implements AfterReturningAdvice interface.
File: AfterAdvisor.java
Create the xml file as in the previous example, you need to change only the advisor class here.
File: applicationContext.xml
File: Test.java
Same as in the previous example.
Output
3) MethodInterceptor (AroundAdvice) Example
Create a class that contains actual business logic.
File: A.java
Same as in the previous example.
Now, create the advisor class that implements MethodInterceptor interface.
File: AroundAdvisor.java
Create the xml file as in the previous example, you need to change only the advisor class here.
File: applicationContext.xml
File: Test.java
Same as in the previous example.
Output
4) ThrowsAdvice Example
Create a class that contains actual business logic.
File: Validator.java
Now, create the advisor class that implements ThrowsAdvice interface.
File: ThrowsAdvisor.java
Create the xml file as in the previous example, you need to change only the Validator class and advisor class.
File: applicationContext.xml
File: Test.java
Output