Advertisement



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


}



Advertisement




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.

Utility.java
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());
	mr.marathonBegins();
	mr.marathonEnds();
}
}


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.


config.beans.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:util="springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       ">


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

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

 
<aop:config>

<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"/>
</aop:aspect>

</aop:config>


</beans>



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


  • We are going to add some JARs files to our Java project, such as -
    • The JARs are associated with the Spring Framework.
    • The JAR associated with the AspectJ to integrate this Spring project with it.
    These JARs are required in order to successfully execute this Spring project as we are working with both Spring Framework and AspectJ using AspectJ Annotations.


    1. All these JARs associated with the Spring Framework are included in its installation folder named libs(within our Spring installation folder). So, we need to add all the JARs in the libs folder to our build path of our Java project.


    2. Note : Those who don't know how to add JARs to the build path of your Java project in Eclipse IDE, please refer to our section Adding JARs to your Spring project in Eclipse.



    3. Next, we are going to add a JAR file named aspectjweaver to the build path of our Java project. This JAR make the AspectJ work weaves or integrate our Spring project with using AspectJ Annotations.

      This JAR file is included in the folder named lib(within our AspectJ installation folder). So, we need to add all this JAR to the build path of our Java project as well.






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.






Execution


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 -






Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement