Spring AOP using XML Configuration + AspectJ

In this article, you will see how to use intercept methods using Aspectj XML Configuration 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 <aop:before/> tag.
  • After : After (finally) advice runs however a matched method execution exits. It is declared using <aop:after/> tag.
  • After-returning : After returning advice runs when a matched method execution returns normally. It is declared using <aop:after-returnin/> tag.
  • After-throwing : After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using <aop:after-throwing/> tag.
  • Around : The final kind of advice is around advice. Around advice runs “around” a matched method execution. It is declared using <aop:around/> tag.

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

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;
 
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">
 
    <bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" />
    <!-- Aspect -->
    <bean id="businessAspect" class="com.kruders.spring.aspect.BusinessAspect" />
     
    <aop:config>
        <aop:aspect ref="businessAspect">
            <aop:pointcut id="businessExp"
                          expression="execution(* com.kruders.spring.aop.BusinessImpl*.*(..))" />
            <aop:before
                    method="before"
                    pointcut-ref="businessExp"/>
            <aop:after
                    method="after"
                    pointcut-ref="businessExp"/>
            <aop:after-returning
                    method="afterReturning"
                    pointcut-ref="businessExp"/>
            <aop:after-throwing
                    method="afterThrowing"
                    pointcut-ref="businessExp"/>
            <aop:around
                    method="around"
                    pointcut-ref="businessExp"/>
       </aop:aspect>
    </aop:config>
</beans>

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

An aspect is declared using the <aop:aspect> element, and the backing bean is referenced using the refattribute as follows:

<aop:config>
   <aop:aspect ref="businessAspect">
   ...
   </aop:aspect>
</aop:config>

3. Aspect Class

Now write a Aspect which will profile our business method.

BusinessAspect.java

package com.kruders.spring.aspect;
 
import org.aspectj.lang.ProceedingJoinPoint;
 
public class BusinessAspect {
    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");
        joinPoint.proceed(); 
        System.out.println("Around after is running");  
    }
}

Note: It’s crucial that you remember to include a call to the proceed() method. If you don’t, then your advice will effectively block access to the advised method.

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 12.1

Figure 12.1 Figure 12.1

You can download the source code of this example here.


No comments yet.

Leave a Reply