Passing parameters to aspect in Spring AOP XML Configuration

In this article, you will see how to pass parameters to an aspect in an XML based mapping with Spring AOP framework.

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:before
                    method="before"
                    pointcut="execution(* com.kruders.spring.aop.BusinessImpl.doSomeThing(String))
                          and args(message)"/>
        </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;
 
public class BusinessAspect {
    public void before(String message) {
        System.out.println("Before method is called");
    }
}

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("Puneet");
    }
}

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

Figure 13.1 Figure 13.1

You can download the source code of this example here.


No comments yet.

Leave a Reply