In this article, you will see how to use intercept methods with Spring AOP framework.
Following advices are supported by Spring AOP :
- Before : Before advice runs before the advised method is executed.
- After-returning : After returning advice runs when a matched method execution returns normally.
- After-throwing : After throwing advice runs when a matched method execution exits by throwing an exception.
- Around : The final kind of advice is around advice. Around advice runs “around” a matched method execution.
First create a new Java Project and configure it as Maven Project. For Reference, Click Here
Add the following dependencies in pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <!-- Proxy class need this library --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> </dependencies>
1. Business Logic and Implementation
We will first write our business logic and then we will add Spring AOP to profile our business methods.
Create an interface BusinessService and write a following method. We will intercept this via Advice
BusinessService.java
package com.kruders.spring.aop;
public interface BusinessService {
void doSomeThing();
}
Now create a class BusinessImpl.java that implements the above interface.
BusinessImpl.java
package com.kruders.spring.aop;
import org.springframework.stereotype.Service;
public class BusinessImpl implements BusinessService {
public void doSomeThing() {
System.out.println("Do Something Here");
int x = 5/0;
}
}
2. Configure Spring AOP
Create Spring-Business.xml and write the following code
Spring-Business.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" /> <!-- Advice --> <bean id="businessAdvice" class="com.kruders.spring.advice.BusinessAdvice" /> <bean id="businessServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="businessService" /> <property name="interceptorNames"> <list> <value>businessAdvice</value> </list> </property> </bean> </beans>
We defined two normal Spring beans – one for our Business class and the other for Business Profiler (i.e. our advice).
We also create a new proxy object named ‘businessServiceProxy‘.
‘target‘ – Define which bean you want to intercept. ‘interceptorNames‘ – Define which class (advice) you want to apply on this proxy /target object.
3. Advice Class
Now write a Advice which will profile our business method.
BusinessAdvice.java
package com.kruders.spring.advice;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
public class BusinessAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice, MethodInterceptor {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("Before method is called (by " + this.getClass().getName() + ")");
}
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) {
System.out.println("After method is called (by " + this.getClass().getName() + ")");
}
public void afterThrowing(ArithmeticException e) throws Throwable {
System.out.println("After throwing method is called");
}
@Override
public Object invoke(MethodInvocation method) throws Throwable {
System.out.println("Around method is called");
System.out.println("Around before is running");
method.proceed();
System.out.println("Around after is running");
return null;
}
}
4. Helper Class
Create Main.java class that loads our Business bean from Spring Context and then calling our business method.
Main.java
package com.kruders.spring.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kruders.spring.aop.BusinessService;
public class Main {
public static void main(String args[]) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Business.xml");
BusinessService businessService = (BusinessService)appContext.getBean("businessServiceProxy");
try {
businessService.doSomeThing();
} catch(ArithmeticException ae) {
}
}
}
The folder structure of the example is shown below in Figure 14.1
Figure 14.1

No comments yet.