Spring AOP AfterReturningAdvice

In this article you will see how to use after returing advice of spring aop. After returning advice runs when a matched method execution returns normally.

For sample code in details, refer the following link. Click Here

1. Advice Class

Now write a Advice BusinessAfterReturning.java which will profile our business method.

BusinessAfterReturning.java

package com.kruders.spring.advice;
 
import java.lang.reflect.Method;
 
import org.springframework.aop.AfterReturningAdvice;
 
public class BusinessAfterReturning implements AfterReturningAdvice{
    @Override
    public void afterReturning(Object returnValue, Method method,
            Object[] args, Object target) {
        System.out.println("After 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.BusinessAfterReturning" />
     
    <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:

Do Something Here
After method is called (by com.kruders.spring.advice.BusinessAfterReturning)

No comments yet.

Leave a Reply