Highly motivated Senior Java/JEE Software Developer with 7+ years of solid industry experience. Excellent team player with an experience working in distributed international teams, using Agile/Scrum methodology. Artur has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

Spring AOP

  • submit to reddit

 Let's define some well known problems

  • Some programming tasks cannot be neatly encapsulated in objects, but must be scattered throughout the code
  • Examples:
    • Logging (tracking program behavior to a file)
    • Profiling (determining where a program spends its time)
    • Tracing (determining what methods are called when)
    • Transaction Management
    • Security Management
  • The result is crosscuting code--the necessary code “cuts across” many different classes and methods

 Above-mentioned problems are easey solvable via AOP.

 Now let's define some AOP concepts

  • Aspect-oriented programming (AOP) provides for simplified application of cross-cutting concerns
  • Joinpoint - Execution point to target
    • Typically, methods
  • Advice - Action taken at a particular joinpoint.
  • Pointcut - A set of joinpoints specifying where advice should be applied (e.g. Regular expression)

 Now it's time to move to Spring AOP

  • Generally, Spring AOP uses the following techniques.
    • Uses Dynamic Proxies if interface available otherwise CGLIB
    • CGLIB creates derived class which proxies requests
  • Less capable than AspectJ
    • does not have field interception
    • only runtime weaving solution is available

 Let's see real Logger example

// Biz Interface
public interface MyBiz {
public void doBizMethod();

//Biz Interface Impl
public class MyBizImpl implements MyBiz {
public void doBizMethod() {
System.out.println("Business Method");

//Around Advice Class
public class MethodLogger implements MethodInterceptor {

public MethodLogger() {}
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
// same with MethodBeforeAdvice
System.out.println("AOP: Before method call!");
try {
// proceed to original method call
Object result = methodInvocation.proceed();
// same with AfterReturningAdvice
System.out.println("AOP: After method call!");
return result;
} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println("AOP: Throw exception!");
throw e;


 Spring Configuration File

<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-2.0.xsd">

<!-- business beans -->
<bean id="myBiz" class="com.attask.seminar.aop.spring.MyBizImpl"/>
<!-- aop -->
<bean id="methodLogger" class="com.attask.seminar.aop.spring.MethodLogger" />
<bean id="myBizProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="myBiz" />
<property name="interceptorNames">
<list> <value>methodLogger</value></list>

// Simple Usage
public static void main(String[] args) {
ClassPathXmlApplicationContext ctxt = new ClassPathXmlApplicationContext("SpringBeans.xml");
MyBiz bizInstance = (MyBiz)ctxt.getBean("myBizProxy");

// Result will be
AOP: Before method call!
Business Method
AOP: After method call!

 Note Try to use advice which is appropriate for your specific case (Before, After, Throws)

 Find attached example under Resources Section.

Article Resources: 
Published at DZone with permission of its author, Artur Mkrtchyan.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Marco Tedone replied on Wed, 2011/05/18 - 3:25am

Hi,I wrote a very similar article yesterday: http://java.dzone.com/articles/aop-made-easy-aspectj-and




Artur Mkrtchyan replied on Wed, 2011/05/18 - 5:17am in response to: Marco Tedone

Hi Marco, Thanks I have seen it. They are similar and different at the same time, anyway I think that both are interesting articles. Thanks, Artur

Philopator Ptolemy replied on Wed, 2011/05/18 - 8:21am

IMHO, annotations make it a lot clearer.

Marco Tedone replied on Wed, 2011/05/18 - 9:43am in response to: Philopator Ptolemy

Yes, I think annotations make it a lot easier to use because one can write pure Java code. By writing aspects, one would also need an aspect compiler.

Mitch Pronschinske replied on Wed, 2011/05/18 - 11:43am

Very nice article Artur.  It's nice to see a growing number of contributions to Javalobby.

Artur Mkrtchyan replied on Wed, 2011/05/18 - 12:27pm in response to: Mitch Pronschinske

Thanks Mitchell. I will do my best to write good articles.

Aamir Iqbal replied on Thu, 2011/05/19 - 8:31am

Well first of all thanks alot for such a good article. I really appreciate your effort.

But on the other hand, i had a bad experience with Aspect Orientation. As i used it in my previous project. 

I am not an expert in its usage, but what i understand it is, Aspect Orientation creates a layer over native Java Compiler. Means firstly code compiles into Aspect orientation, then it is given to Java Compiler. Due to this, i faced some issues in Eclipse IDE. eg. Eclipse highlights a variable or a statement underlined RED as an error. But when i click on Run, it works perfectly fine.

While in some cases, the issue was totally reverse. Means the Eclipse IDE does not show any error being highlighted. But when i Click on Run then it prints a huge list of errors on console.

These things frustrated me alot. and eventually i started disliking Aspect Orientation. I dont know whether it was due to Aspect Nature or it was something else, as i was new to Aspect Orientation.



Amir Iqbal



Mehdi Heidarzadeh replied on Fri, 2011/05/27 - 2:51am

Hi Aamir I think it's not the problem of AOP! it's the problem of Eclipse IDE!!! Try to use another tools to write code like NetBeans or IntelliJ IDEA. I'm sure it will work. I think programming java without AOP is not possible!!! Of course you might say: Hey no! it's possible!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.