Spring AOP AroundAdvice

In this article you will see how to use around advice of spring aop. The final kind of advice is around advice. Around advice runs “around” a matched method execution.

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

1. Advice Class

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

BusinessAroundMethod.java

package com.kruders.spring.advice;
 
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
 
public class BusinessAroundMethod implements MethodInterceptor{
    @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;
    }
}

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.BusinessAroundMethod" />
     
    <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:

Around method is called
Around before is running
Do Something Here
Around after is running


No comments yet.

Leave a Reply