< Prev
Next >

Spring with AspectJ by Schema

In this tutorial, we are going to explain how to configure the Spring Framework to work with AspectJ using schema provided by Spring Framework. For this, we are going to declare and define some important AspectJ properties such as aspect, pointcut, joinpoint and some simple advices in a configuration xml file, instead of declaring these AspectJ properties within a Java class(as we did in our previous tutorial to configure Spring with AspectJ using AspectJ Annotations).

In order to make this Spring project work with AspectJ using schema provided by Spring, we are going to include an http://www.springframework.org/schema/aop namespace within this configuration xml file.

Creating the Java class - MarathonRunner

We are going to create a java class named MarathonRunner within the decodejava package and this class contains - Besides this, we are also going to define a couple of getter and setter methods within this class to set the above mentioned properties of MarathonRunner class.

Besides these getter and setter methods, we are going to define two more methods i.e. marathonBegins() and marathonEnds(), which are going to become our joinpoints. Before the execution of these methods(joinpoints), we are going to apply the pointcuts and advices of AspectJ (using schema), later on.

package decodejava;

public class MarathonRunner
String name;
int age;

//Getter method for age
public int getAge() {
	return age;

//Setter method for age
public void setAge(int age) {
	this.age = age;

//Getter method for name
public String getName()
	return name;

//Setter method for name
public void setName(String name) 
	this.name = name;

public void marathonBegins()
System.out.println("Marathon runner has started the race");

public void marathonEnds()
System.out.println("Marathon runner has finished the race");


Class defining pointcuts and advices

Next, we are going to add another Java class named MarathonTimeTracker and which is going to be associated with a few AspectJ properties, such as - pointcut, joinpoints and advices using the schema provided by Spring Framework through a configuration xml file(to be created later on in this tutorial).

package decodejava;

public class MarathonTimeTracker
	public void trackingBegins()
	System.out.println("Marathon has started");
	System.out.println("Tracking the marathon has begun");

	public void trackingEnds()
	System.out.println("Marathon has ended");
	System.out.println("Tracking the marathon has ended");


Adding the Utility class that calls the Spring API

Next, we are going to create another class named - Utility, which is a simple java class.

package decodejava;	

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Utility
public static void main(String[] ar)
	ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
	MarathonRunner mr = context.getBean("MarathonRunnerBean", MarathonRunner.class);
	System.out.println("Name of the Marathon Runner - " + mr.getName());
	System.out.println("Age of the Marathon Runner - " + mr.getAge());

The Utility class uses the ApplicationContext container(an interface) of Spring Framework by creating its instance using its implemented class FileSystemXmlApplicationContext, which loads the configuration xml file - config.beans.xml and does the following -

Adding a configuration file

Next, we are going to add a configuration file to our project. This configuration document is an Extensible Markup Language(XML) file, ending with .xml extension and we are going to name file as config.beans.xml.

In order to make this Spring project work with AspectJ using schema provided by Spring, we are going to include an http://www.springframework.org/schema/aop namespace within this configuration file.

In this file, we have configured a MarathonRunner bean with a unique id and with its property named name and age. These properties will be assigned a value by the Spring Container using their respective setter methods, when the MarathonRunner bean is created by it, using the configuration xml file.

We have also configured a MarathonTimeTracker bean with a unique id, which is referenced by the aspect declared within <aop:config> configuration element.

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

<bean id="MarathonRunnerBean" class="decodejava.MarathonRunner">
<property name="name" value="Runner1"></property>
<property name="age" value="27"></property>

<bean id="MarathonTimeTrackerBean" class="decodejava.MarathonTimeTracker"></bean>


<aop:pointcut id ="trackingBeginsPointCut1"
expression ="execution(public void decodejava.MarathonRunner.marathonBegins())"/>

<aop:pointcut id ="trackingEndsPointCut2"
expression ="execution(public void decodejava.MarathonRunner.marathonEnds())"/>

<aop:aspect ref="MarathonTimeTrackerBean">
<aop:before method="trackingBegins()" pointcut-ref="trackingBeginsPointCut1"/>
<aop:after-returning method="trackingEnds()" pointcut-ref="trackingEndsPointCut2"/>



This mapping document has a parent <beans> tag as the root element and its individual child elements, where each child element stats with a <bean> tag, containing all the attributes such as -

Next, the <aop:config> configuration element is used define aspect oriented programming elements such as -

Adding JARs

Directory Structure of Project

The picture above depicts how and where to arrange classes and interfaces comprising this Spring Project, in a specific directory structure.

Project Folder - SpringWithAspectJBySchema is the name of our Project and it is a top-level directory.


Finally, after executing the Utility class, you will get the following output within the Console window. This output displayed below shows, how the Utility class has used the ApplicationContext container of Spring Framework to load the configuration xml file - config.beans.xml, in order to access the beans specified in it, instantiate the MarathonRunner class.

Whenever the method of MarathonRunner are called, the pointcuts and advices tracking them are called and their associated methods of MarathonTimeTracker of class are executed(configured in xml).

Aug 03, 2018 6:14:47 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1eb44e46: startup date [Fri Aug 03 18:14:47 2018]; root of context hierarchy
Aug 03, 2018 6:14:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Marathon has started
Tracking the marathon has begun
Marathon runner has started the race
Marathon runner has finished the race
Marathon has ended
Tracking the marathon has ended

And, this concludes configuring Spring Framework with AspectJ using schema provided by Spring Framework and its <aop:config> element.

Please share this article -

< Prev
Next >
< Spring-AspectJ with Annotation
Spring-AspectJ with DTD >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next