In this article you will see how to use method before advice of spring aop. Before advice runs before the advised method is executed.
For sample code in details, refer the following link. Click Here
1. Advice Class
Now write a Advice BusinessBeforeAdvice.java which will profile our business method.
BusinessBeforeAdvice.java
package com.kruders.spring.advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BusinessBeforeAdvice implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("Before method is called (by " + this.getClass().getName() + ")");
}
}
2. Configure Spring AOP
Create Spring-Business.xml and write the following code
Spring-Business.xml
<bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" /> <!-- Advice --> <bean id="businessAdvice" class="com.kruders.spring.advice.BusinessBeforeAdvice" /> <bean id="businessServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="businessService" /> <property name="interceptorNames"> <list> <value>businessAdvice</value> </list> </property> </bean>
3. Output
When you run the above example you’ll get an output like:
Before method is called (by com.kruders.spring.advice.BusinessBeforeAdvice)
Do Something Here

No comments yet.