The Artima Developer Community
Sponsored Link

Java Community News
Cross-Cutting Concerns with Spring 2.0 AOP

1 reply on 1 page. Most recent reply: Jan 8, 2007 7:40 PM by James Watson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Cross-Cutting Concerns with Spring 2.0 AOP Posted: Jan 8, 2007 3:26 PM
Reply to this message Reply
Summary
A recent JavaWorld article by Ganesh Ghag demonstrates aspect-oriented development with Spring 2.0's AOP framework. The article shows how to move cross-cutting concerns from mainline application code into their own POJOs, and use Spring 2.0's configuration files to apply those aspects to business logic.
Advertisement

Aspect-oriented programming, or AOP, excited much interest a few years ago, but to date relatively few developers have coded applications in an AOP style. One of AOP's key promises is that it allows functionality that impacts many parts of an application, such as logging, security, or transactionality, to be removed from mainline application code into their own classes. A small AOP glue code can then be used to apply such cross-cutting concerns to business logic at the appropriate time.

Such AOP glue, however, required that developers either rely on programming language extensions, such as AspectJ, or some other external configuration files, both of which could potentially reduce the portability of code across application servers.

With AOP as one of its cornerstone features from the outset, Spring 2.0 may be the most effective way to benefit from AOP in enterprise applications, according to a recent JavaWorld article by Ganesh Ghag, Implement crosscutting concerns using Spring 2.0 AOP.

Ghag starts out by illustrating how Spring AOP allows moving cross-cutting concerns into their own POJOs, leading to cleaner and more focused code. A typical method may start out mixing logging, security, and transactionality, for instance:

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

Ghag shows that the above method can be written to focus on just the business logic, moving the cross-cutting concerns into their own POJOs:

public String doSomething(String input) {            
    //some business logic
    return input;
}

The price of factoring out cross-cutting functionality is a small amount of XML configuration data required by Spring 2.0 AOP. To implement logging, for instance, Ghag defines a logging aspect as follows:

public class MyLoggingAspect {
    public Object log(ProceedingJoinPoint call) throws Throwable {
      System.out.println("from logging aspect: entering method [" + call.toShortString()
                            +"] with param:"+call.getArgs()[0] );
        Object point =  call.proceed();
        System.out.println("from logging aspect: exiting method [" + call.toShortString()   
                            + "with return as:" +point);        
        return point;
    }
}

The following XML configuration will apply this aspect to the business logic:

<bean id="LoggingAspect"  class = "com.myorg.springaop.examples.MyLoggingAspect"/>
<aop:config>
      ...
      ...
      ...
      <aop:aspect ref="LoggingAspect">
         <aop:pointcut id="myCutLogging"
                    expression="execution(* com.myorg.springaop.examples.MyService*.*(..))"/>
         <aop:around pointcut-ref="myCutLogging" method="log"/>
      </aop:aspect>
</aop:config>

What do you think of Spring 2.0's AOP framework?


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Cross-Cutting Concerns with Spring 2.0 AOP Posted: Jan 8, 2007 7:40 PM
Reply to this message Reply
Does AOP arise from the inability to create proxy-objects? Or said another way, would AOP offer anything to a language like SmallTalk, Python or Ruby where proxy Objects can be easily created?

Flat View: This topic has 1 reply on 1 page
Topic: It's Official: Jini = Apache River Previous Topic   Next Topic Topic: What's On Your Java SE 7 Wish List?


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us