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


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.


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

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


package com.kruders.spring.aop;
import org.springframework.stereotype.Service;
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


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    <aop:aspectj-autoproxy />
    <bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" />
    <!-- Aspect -->
    <bean id="businessAspect" class="com.kruders.spring.aspect.BusinessAspect" />

<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.


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;
public class BusinessAspect {
    @Pointcut("execution(* com.kruders.spring.aop.BusinessImpl*.*(..))")
    public void businessMethods() { }
    public void before() {
        System.out.println("Before method is called");
    public void after() {
        System.out.println("After method is called");
    public void afterReturning() {
        System.out.println("After returning method is called");
    public void afterThrowing() {
        System.out.println("After throwing method is called");
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
         System.out.println("Around method is called");
         System.out.println("Around before is running!");
         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.


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");

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