Spring AOP MethodBeforeAdvice

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.


package com.kruders.spring.advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BusinessBeforeAdvice implements MethodBeforeAdvice{
    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


<bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" />
    <!-- Advice -->
    <bean id="businessAdvice" class="com.kruders.spring.advice.BusinessBeforeAdvice" />
    <bean id="businessServiceProxy"
    <property name="target" ref="businessService" />
    <property name="interceptorNames">

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.

Leave a Reply