Spring AOP using Annotation + AspectJ

In this article, you will see how to use intercept methods using Aspectj Annotation with Spring AOP framework.

5 kinds of advice that spring supports:

  • Before : Before advice runs before the advised method is executed. Before advice is declared in an aspect using the @Before annotation.
  • After : After (finally) advice runs however a matched method execution exits. It is declared using the @Afterannotation.
  • After-returning : After returning advice runs when a matched method execution returns normally. It is declared using the @AfterReturning annotation.
  • After-throwing : After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using the @AfterThrowing annotation.
  • Around : The final kind of advice is around advice. Around advice runs “around” a matched method execution. It is declared using the @Around annotation.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<properties>
    <spring.version>3.1.1.RELEASE</spring.version>
    <aspectj.version>1.7.0</aspectj.version>
</properties>
  
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
</dependencies>

1. Business Logic and Implementation

We will first write our business logic and then we will add Spring AOP to profile our business methods.

Create an interface BusinessService and write a following method. We will intercept this via AspectJ Annotation.

BusinessService.java

package com.kruders.spring.aop;
 
public interface BusinessService {
    void doSomeThing();
}

Now create a class BusinessImpl.java that implements the above interface.

BusinessImpl.java

package com.kruders.spring.aop;
 
import org.springframework.stereotype.Service;
 
@Service("businessService")
public class BusinessImpl implements BusinessService {
    public void doSomeThing() {
        System.out.println("Do Something Here");
    }
}

2. Configure Spring AOP and AspectJ support

Create Spring-Business.xml and write the following code

Spring-Business.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"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 
    <aop:aspectj-autoproxy />
    <bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" />
    <!-- Aspect -->
    <bean id="businessAspect" class="com.kruders.spring.aspect.BusinessAspect" />
</beans>

<aop:aspectj-autoproxy /> will create an AnnotationAwareAspectJAutoProxyCreator in Spring context and will automatically proxy beans whose methods match the pointcuts defined with @Pointcut annotations in @Aspect-annotated beans. If you want to use the element you need to include the aop namespace in your Spring configuration file.

And then we defined two normal Spring beans – one for our Business class and the other for Business Profiler (i.e. our aspect).

3. Aspect Class

Now write a Aspect which will profile our business method. @Aspect annotation is used to declare that the class works as an aspect.

BusinessAspect.java

package com.kruders.spring.aspect;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
 
@Aspect
public class BusinessAspect {
    @Pointcut("execution(* com.kruders.spring.aop.BusinessImpl*.*(..))")
    public void businessMethods() { }
  
    @Before("businessMethods()")
    public void before() {
        System.out.println("Before method is called");
    }
    @After("businessMethods()")
    public void after() {
        System.out.println("After method is called");
    }
    @AfterReturning("businessMethods()")
    public void afterReturning() {
        System.out.println("After returning method is called");
    }
    @AfterThrowing("businessMethods()")
    public void afterThrowing() {
        System.out.println("After throwing method is called");
    }
    @Around("businessMethods()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
         System.out.println("Around method is called");
         System.out.println("Around before is running!");
         joinPoint.proceed(); 
         System.out.println("Around after is running!");    
    }
}

Using @Around annotation, we have defined a Around advice which will be invoked before and after our business method. i.e. @Around advice will wrap our business method. As you can see in the above code, joinPoint.proceed(). This is when our business method gets called from @Around advice.

4. Helper Class

Create Main.java class that loads our Business bean from Spring Context and then calling our business method.

Main.java

package com.kruders.spring.core;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.kruders.spring.aop.BusinessService;
 
public class Main {
    public static void main(String args[]) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Business.xml");
        BusinessService businessService = (BusinessService)appContext.getBean("businessService");
        businessService.doSomeThing();
    }
}

The folder structure of the example is shown below in Figure 11.1

Figure 11.1 Figure 11.1

You can download the source code of this example here.


No comments yet.

Leave a Reply