http://www.developer.com/http://www.developer.com/java/implementing-aop-in-spring.html
Aspect Oriented Programming (AOP) has always been a fundamental component of the Spring framework. This programming model used to be a bit complex due to the exposure of the low level constructs to the Spring developer. Perhaps, for this reason programming Spring AOP rested in the hands of advanced developers. Lately, a lot has changed - Spring has evolved and so has AOP. In this article I will resort to a canonical explanation of AOP and on how to implement it in Spring. So, before getting hands on - a little detour. In case of Functional Programming (FP), problems are broken down into a module of functions, which carry out a particular unit of work. Each function talks to another through function calls. The whole concept is to separate the problem domain with a cohesive module of function. Now, imagine a situation of a complex network of function calls. You get a bowl of spaghetti code! Spaghetti code In Object Oriented Programming (OOP) the thought process of designing an application gets a paradigm shift. Different tasks are no longer modeled as a unit of function but rather as an individual object, which mirrors a different entity in a problem space. As a result designing complex code got a much cleaner separation of concern. But still... A couple of problems persist. Whatever the case, it is not a very good idea to design in the manner described. In the above case, observe that the most used object in the arena is actually the most unimportant one. The Logger object adds nothing to the business concern. It only supports the cause yet it gets the maximum attention from the object interaction. OOP is just fine but another level of modularity is sought to address this issue of pure separation of cross-cutting concern. AOP leveraged the modularity of OOP paradigm to a whole new level. It provides separation of crosscutting concerns by introducing a new unit of modularization called an aspect. The aspect focuses on a particular crosscutting functionality and does not burden the core business classes of the application with crosscutting concerns. This is done with aspect weaver that composes the final system by combining core classes and crosscutting aspects through a process called weaving. AOP further refurbished OOP, which are easier to design, implement, and maintain. The core areas of Spring are dependency injection, enterprise service abstraction and aspect oriented programming. Dependency injection allows disparate components to be wired into a fully working application in declarative manner, enterprise service abstraction isolates stable application logic from volatile infrastructure code and AOP lets you separate the implementation of cross-cutting concerns from business concerns. This trio can be well integrated to offer a compelling solution in Spring. Software Dependencies: For Aspectj programming For Spring framework Java SE and Eclipse (Note: any other IDE would be fine but here Eclipse is used). Install Java SE and Eclipse. Create user libraries in Eclipse with the following jars and include the library as follows. Create aspectj user library and import the following jars 1. aopalliance-x.x.x.jar 2. cglib-nodep-x.x.x.jar 3. aspectjrt.jar, aspectjweaver.jar (you may have to unzip aspectj-x.x.x.jar to get these two files) 4. asm-x.x.jar Create Spring user library and include the following jars 1. commons-logging-x.x.x.jar 2. All jar files from spring-framework-x.x.x/dist Once you are done creating user library in Eclipse, you can reuse the library in any other project in which you wish to include the aspectj feature. Note: User Library can be created in Eclipse from Windows->Preference, Expand Java->Build Path->User Libraries from the lists in the left of preference window. The example contains four classes: Employee and Department as the model class, Main as the starting point of the application and LoggingAspect as the aspect class. Main Class demonstrates dependency injection in Spring. Configuration file spring.xml demonstrates dependency injection and AOP configuration. LogginAspect class demonstrates the implementation of AOP in Spring. Note that it is nothing more than a simple POJO with the annotation @Aspect. Inside this class one can write as many advices (advice is a AOP terminology that means nothing more than a function prefixed with AOP annotation) as one wants to using the annotation like @Before, @After, @Within, @Around, @Pointcut etc. @Before annotation simply means that beforeAdvice advice would run before any public String getName() function is executed. There are other variations of @Before annotation such as: The difference is that the advice now runs only before getName function of Employee class. Using wild cards: calls any public function with any return type with no argument but function name prefixed by 'get'. calls any public function with any return type with zero/more argument but function name prefixed by 'get'. calls any public function with any return type with one/more argument but function name prefixed by 'get'. For more examples and explanation visit the Spring aspect-oriented programming documentation. There are several occasions where AOP can be very handy, some of the most common use cases can be Logging and Profiling. There are innumerable such AOP usages. The citations above just scratche the surface. Spring is a popular lightweight framework for enterprise applications. AOP system based on interceptors and the proxy design pattern is included in the framework to replenish the needs of enterprise applications. Prior to Spring 2.0, Spring AOP was more complex. The new programming model based on AspectJ offers a better programming experience and enables programmers to write better code and customize aspects without difficulty. Once one has a fair grasp on the concept of dependency injection and AOP, the learning curve of Spring implementation of aspect-oriented programming in almost flat.
Implementing AOP in Spring
February 25, 2013
Functional Programming

Object Oriented Programming

Aspect Oriented Programming
AOP in Spring Framework
AOP implementation in Spring
Configuring Your Project for AOP
Examples
package org.simpleaop.model;
public class Employee {
private String name;
...
//Getters and Setters
}
package org.simpleaop.model;
public class Department {
private String location;
...
//Getters and Setters
}
package org.simpleaop.app;
//...import statements
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Employee emp = context.getBean("employee", Employee.class);
emp.setName("Jerry Mouse");
Department dept = context.getBean("department", Department.class);
dept.setLocation("Disney Land");
System.out.println("Name :"+emp.getName());
System.out.println("Department :"+dept.getLocation());
}
}
<?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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy />
<bean name="employee" class="org.simpleaop.model.Employee"/>
<bean name="department" class="org.simpleaop.model.Department"/>
<bean name="loggingAspect" class="org.simpleaop.aspect.LoggingAspect" />
</beans>
package org.simpleaop.aspect;
//..import statements
@Aspect
public class LoggingAspect {
@Before("execution(public String getName())")
public void beforeAdvice(){
System.out.println("Before advice. Runs before the function called");
}
//...other advices
}
@Before("execution(public
String org.simpleaop.Employee.getName())")
@Before("execution(public * get*())")
@Before("execution(public * get*(..))")
@Before("execution(public * get*(*))")
AOP Usage
Conclusion