Spring AOP Around Advice Annotation Example

0

In this example we will look into around advice. We have see examples of @Before advice, @After advice, @AfterReturning and @AfterThrowing advice. Around advice combines all the advice into one so it is considered most powerful of all the advice types as it offers an extreme flexibility because of its generic character.

Example of Spring AOP Around Advice Annotation

In order to create around advice, you must use the @Around annotation.
We will apply around advice on method greet(greeting) of TestBean. Method greet(greeting) simply returns the greeting argument. If greeting is null, it will throw NullPointerException.

TestBean:

package com.javarticles;

public class TestBean {
    public String greet(String greeting) {
        System.out.println("greet(" + greeting + ") called");
        if (greeting == null) {
            System.out.println("message is null");
            throw new NullPointerException();
        }
        return greeting;
    }    
}

Using @Around annotation, we will apply aroundAvice as around advice on greet(String) pointcut indicated by expression execution(String greet(String)).
In order for the around annotation to work, we need to make sure of the below two things:

  1. The argument type of the join point must be ProceedingJoinPoint. It’s a subinterface of JoinPoint that allows you to control when to proceed with the original join point.
  2. We must remember to explicitly invoke proceed() so that the proxied method will be invoked

AopBean:

package com.javarticles;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AopBean {

    @Around("execution(String greet(String))")
    public String aroundAvice(ProceedingJoinPoint joinPoint) {
        String result = null;
        System.out.println("@Around AOP aroundAvice(): before execution");
        try {
            result = "Hello " + joinPoint.proceed() + "!";
            System.out
                    .println("@Around AOP aroundAvice(): after execution, target bean's result: "
                            + result);
        } catch (Throwable e) {
            System.out
                    .println("@Around AOP aroundAvice(): afterThrowing: " + e);
        }
        return "Greeting is NULL";
    }
}

Let’s configure spring XML context.
Declare the beans TestBean and AopBean.
Include <aop:aspectj-autoproxy/>.

applicationContext.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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<bean id="testBean" class="com.javarticles.TestBean"/>
    <aop:aspectj-autoproxy/>
	<bean id="aopBean" class="com.javarticles.AopBean"/>
	
</beans>

We will load the spring context. Call testBean.greet() with some value and then a null value.

SpringAopAroundAdviceExample:

package com.javarticles;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAopAroundAdviceExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            TestBean testBean = (TestBean) context.getBean("testBean");
            System.out.println("Call testBean.greet(Joe)");
            System.out.println("Final result of testBean.greet(Joe)=>"
                    + testBean.greet("Joe"));
            System.out.println("Call testBean.greet(null)");
            System.out.println("Final result of testBean.greet(null)=>"
                    + testBean.greet(null));
        } finally {
            context.close();
        }
    }
}

As you can see, around advice rolls all other advices into one. You can do something before the proxy of target bean is called. Call the proxy bean’s method. Next, you can further process after the proxy returns you the result. If any exception, it can be caught and dealt in the catch section. You can also return a totally different result than what your target bean’s method returned.

Output:

Call testBean.greet(Joe)
@Around AOP aroundAvice(): before execution
greet(Joe) called
@Around AOP aroundAvice(): after execution, target bean's result: Hello Joe!
Final result of testBean.greet(Joe)=>Greeting is NULL
Call testBean.greet(null)
@Around AOP aroundAvice(): before execution
greet(null) called
message is null
@Around AOP aroundAvice(): afterThrowing: java.lang.NullPointerException
Final result of testBean.greet(null)=>Greeting is NULL

Download the source code

This was an example about spring AOP Around Annotation.

You can download the source code here: springAopAroundAdviceAnnotationExample.zip
Share.

Comments are closed.