Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Implement crosscutting concerns using Spring 2.0 AOP

Spring 2.0 offers excellent user-friendly AOP capabilities

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Most developers will acknowledge how implementing crosscutting concerns such as logging, auditing, security and transactionality can adversely affect business logic implementation. Such concerns "seem" to increase the complexity of existing business logic, at times making it difficult if not impossible to clearly distinguish business logic from the crosscutting concern implementation.

I had been hearing about aspect-oriented programming (AOP) being a solution for implementing crosscutting concerns. But having read through early AOP tools documentation, I was dissuaded from using AOP in my projects' recommendations, primarily because of a concern that average developers might not "get" AOP concepts, and the overall ability to debug applications, and maintain them, might be restricted to those few who understood AOP.

Recently released Spring 2.0 provides excellent, easy-to-use AOP capabilities. Spring AOP is now remarkably easy to use for the average Java developer with traditional object-oriented programming skills. This story shows how Spring 2.0 AOP can be effectively used to implement crosscutting concerns in a typical software project. We will consider the following crosscutting concerns for implementation:

  • Security
  • Logging
  • Transactionality

Let us start with an example code snippet:

 public String doSomething(String input)
{
      //Logging
        System.out.println("entering business method with:"+input);
        
        //Security check for authorization of action (business-method) 
        
        //transactionality
        try
        {
            //Start new session and transaction
            
            //Some business logic
            
            //Commit transaction
        }
        catch(Exception e)
        {
            //Rollback transaction 
        }
        finally
        {
            //Close session
        }
        
        //Logging
        System.out.println("exiting business method with:"+input);

      return input;
}

As can be seen, the actual business logic in the business method appears hopelessly lost in the crosscutting concerns code for security, transactionality and logging. My intent is to demonstrate the ability to implement these concerns as cleanly segregated from the business logic as might be possible.

Before we begin, let's review a few basic definitions used in aspect-oriented programming and their implementation in Spring AOP.

AOP basics

  • Aspect: A modularized implementation of a software concern that cuts across various objects in a software implementation. Logging is a good example of an aspect. In Spring AOP, aspects are nothing more than regular Spring beans, which themselves are plain-old Java objects (POJO) registered suitably with the Spring Inversion of Control container. The core advantage in using Spring AOP is its ability to realize the aspect as a plain Java class.
  • Join point: A point during program execution, such as a method executing or an exception being handled. In Spring AOP, a join point exclusively pertains to method execution only, which could be viewed as a limitation of Spring AOP. However, in reality, it is enough to handle most common cases of implementing crosscutting concerns.
  • Advice: Information about "when" an aspect is to be executed with respect to the join point. Examples of types of advice are "around," "before," and "after." They specify when the aspect's code will execute with respect to a particular join point.
  • Pointcut: A declarative condition that identifies the join points for consideration during an execution run. Pointcut is specified as an expression. Spring AOP uses the AspectJ pointcut expression syntax. An example pointcut expression is: execution(* com.myorg.springaop.examples.MyService*.*(..)). Asterisks in the expression refer to wildcards, as is conventional.

Let's try and implement each of the concerns using Spring 2.0 AOP.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (5)
Login
Forgot your account info?

Good articleBy Anonymous on November 18, 2009, 11:25 amGood article indeed!

Reply | Read entire comment

Excellent Article!!By Anonymous on November 17, 2009, 4:33 pmExcellent Article!!

Reply | Read entire comment

Nice OneBy Anonymous on July 15, 2009, 1:54 pmits helpful to understand live example which i can test my self, after reading tutorial and example i have more sense of AOP

Reply | Read entire comment

Everything of this sample is good for my understanding, but i caBy Anonymous on April 26, 2009, 10:52 pmEverything of this sample is good for my understanding, but i can't find out EntityServiceSpringTest in source code, please help.

Reply | Read entire comment

AOP in MVCBy Anonymous on November 15, 2008, 7:41 amvery good article.

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources