TechAlpine – The Technology world

Implementing AOP using Spring framework

Spring AOP

Spring AOP – How to implement Aspect Oriented Programming?

Overview

The Aspect Oriented Programming (AOP) offers a new way of looking at how software programming can be done. When compared to the Object Oriented Programming (OOP), the AOP does not represent a total or drastic departure to how programming is done. AOP just represents a new philosophy of programming and aims at improving efficiency between the different components of program logic. According to AOP principles, business objects within program logic should not ideally have support methods such as on transaction, security and logging because that not only creates clutter but also reduces efficiency because of excessive and redundant coding. The object methods are too reliant on support objects. AOP prescribes not only separating the support methods from all business objects but also ensure that redundant coding is eliminated. So, business objects in the program logic do not need to call separate aspects because that requires separate coding.  A configuration representing all aspects does the job of providing support to all the business objects in the program logic. The AOP comprises a number of components that in their own little ways help implement the basic principles of AOP.








Central concepts of AOP

There are a few issues or inefficiencies in how the concept of Object Oriented Programming (OOP) works in real life. For example, too much coding could be required even if some common support methods such as transaction, logging and security are taken out of the business objects and separate objects for such support tasks are created. Also, cross cutting creates too much dependency on the supporting objects. The central idea of AOP is to create separate aspects on transaction, security and logging and a configuration files for the support aspects. The configuration file is configured with scenarios of different methods within the objects calling the aspects. The main difference is that instead of configuring or writing codes for separate aspects and methods for calling the aspects, the programmer needs to just configure the aspect configuration file once. The main advantage with this approach is that the programmer effort at coding is reduced; dependencies on cross cutting objects or aspects are reduced and the main business objects in the program logic can do their core job and leverage the aspects whenever required.  AOP comprises a certain number of components that makes managing objects and their relationships with the aspects easier to handle.



Goals of AOP

The main goals of AOP are:

Reducing coding effort

If all the methods in all the business objects of the program logic reference the transaction, security and logging aspects, configuration of such references entails huge coding efforts. The aspect configuration file contains a one-time configuration that allows all methods to reference the aspects. Even if the configuration file needs to be updated multiple times, that still reduces coding significantly.

Reducing clutter in objects

The methods written for transaction, security and logging in the business objects are removed and taken to the dedicated aspects. This reduces the clutter in the business objects within the program logic.

Improving program logic efficiency

When the support methods are written within the business objects, it is reflective of poor design principles and that can impact the performance of the software code. AOP seeks to remove the support methods and create aspects out of them separately so that the business objects can call the aspects whenever required. This allows the business objects to focus on their core tasks and the support aspects do their jobs.

Components of AOP

The main components of AOP are described below:

  • Join Point: In Spring AOP, a join point represents a point during the execution of a program when execution of a method or handling of an exception is done.
  • Advice: Advice represents the action taken by an aspect at a join point. There are different types of advises such as before, after returning, after throwing, after and around. Example: The before advice acts before a join point happens but cannot prevent the execution.
  • Pointcut: It is a predicate that matches all join points. An advice is related with a pointcut expression and executes at any join point that is matched by the pointcut. Example: the execution of a method with a particular name.
  • Weaving: Weaving is a component that links aspects with other types of applications or objects to create an advised object.









Environment setup:

Setting up the environment for Spring based application development involves just three major steps.

  • Setting up Java Development Kit

Download JDK from the Oracle site and then, install and configure it. Then the PATH and JAVA_HOME environment variables must be set.

  • Setting of Eclipse IDE

Eclipse can be downloaded from the official website. Once downloaded, unpack the binaries and then set the PATH as well.

  • Setting up Spring libraries

The Spring libraries may be obtained from http://repo.spring.io/release/org/springframework/spring/. Again, it is important to set the CLASSPATH correctly.

  • Set up AOP specific libraries

Download aspectjrt.jar, aspectjweaver.jar and aspectj.jar and place them in the lib folder where other spring Jars are kept.

Now your environment is ready to start developing Spring with Java applications.

Sample application:

In this section we will work with a Spring AOP example where the aspects are set based on a XML file.

Listing 1: This is the bean file

package com.dashboardapp.aopdemo;

public class Employee {

private String empname;

public void setEmpname(String empname) {

this.empname = empname;

}

public String getEmpname() {

System.out.println(“Emp name is : ” + empname );

return empname;

}

}

Following is the advice file. We will have only two advice which will be executed before and after a particular method execution.








Listing 2: This is the file defining the advice

package com.dashboardapp.aopdemo;

public class LogAOP {

//Execute before a selected method execution

public void beforeAdvice(){

System.out.println(“Before Advice – Start employee name setup”);

}

//Execute after a selected method execution

public void afterAdvice(){

System.out.println(“After Advice – End employee name setup”);

}

} 

Following is the most important file which will have declaration of beans, point-cuts, and aspects. This is the XML file for configuration of the entire aspect.

Listing 3: XML file for configuration 

<?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”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>

<aop:config>

<aop:aspect id=”log” ref=”aoplog”>

      <aop:pointcut id=”selectAll”

expression=”execution(* com.dashboardapp.aopdemo.Employee.getEmpname(..))”/>

<aop:before pointcut-ref=”selectAll” method=”beforeAdvice”/>

<aop:after pointcut-ref=”selectAll” method=”afterAdvice”/>

</aop:aspect>

</aop:config>

<!– Definition for Employee bean –>

<bean id=”emp” class=”com.dashboardapp.aopdemo.Employee”>

<property name=”empname”  value=”Nicholas” />

</bean>

   <!– Definition for logging aspect –>

<bean id=”aoplog” class=”com.dashboardapp.aopdemo.LogAOP”/>

</beans> 

Listing 4: This is the main Java file

package com.dashboardapp.aopdemo;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainAOP {

public static void main(String[] args) {

ApplicationContext context = new FileSystemXmlApplicationContext(

“WebContent/WEB-INF/aopBean.xml”);

Employee emp = (Employee) context.getBean(“emp”);

emp.getEmpname();

}

}

Now, if we run the main file the following output will be displayed.

 

AOP output

Image 1: Showing output

Conclusion

As already stated, AOP is not a drastically different programming philosophy from OOP concepts. It retains the structure of OOP largely and mainly focuses on the support objects. That way, the program logic can also be said to be a blend of AOP and OOP. However, one critique about AOP is that it does not do much in improving design efficiency when an OOP program logic design becomes messy or cluttered because of too any interrelated objects.


 

Please follow and like us:
0

Leave a Reply

Your email address will not be published. Required fields are marked *


1 × 1 =

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+
http://techalpine.com/implementing-aop-using-spring-framework">
LinkedIn