ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1
Pages: 1, 2

Reusing Aspects

The Method Tracing aspect can be extended to provide a slightly more complex Logging aspect. The Logging aspect provides a good example of reuse, as many of the characteristics needed of a Logging aspect have already been developed in the Method Tracing aspect.



In this example the Logging aspect extends the Method Tracing aspect in order to display additional information concerning exceptions that have been raised during an application's execution.

A few changes are needed to the application in order to fully exercise the Logging aspect. The BusinessLogicException exception class provides an exception that can be raised by the new void bar() method on the IBusinessLogicInterface interface and BusinessLogic implementation class.

public class BusinessLogicException 
   extends Exception
{

}

public interface IBusinessLogic
{
    public void foo();
   
    public void bar() 
      throws BusinessLogicException;
}

public class BusinessLogic 
   implements IBusinessLogic
{
    public void foo() 
    {
        System.out.println(
          "Inside BusinessLogic.foo()");
    }
     
    public void bar() 
       throws BusinessLogicException
    {
        System.out.println(
        "Inside BusinessLogic.bar()");
          throw new BusinessLogicException();
    }
}

The MainApplication class now makes an additional call to the void bar() method and handles the checked exceptions that are potentially raised by that method.

import org.springframeworkcontext.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApplication
{
    public static void main(String [] args)
    {
        // Read the configuration file
        ApplicationContext ctx =
          new FileSystemXmlApplicationContext(
            "springconfig.xml");

       //Instantiate an object
       IBusinessLogic testObject = 
          (IBusinessLogic) ctx.getBean(
            "businesslogicbean");

       //Execute the public methods of the bean
       testObject.foo();
      
       try
       {
           testObject.bar();
       }
       catch(BusinessLogicException ble)
       {
           System.out.println(
             "Caught BusinessLogicException");
       }
    }
}

The TracingBeforeAdvice and TracingAfterAdvice advice from the Method Tracing aspect can be entirely reused. The LoggingThrowsAdvice class provides the advice for the new exception logging.

import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;

public class LoggingThrowsAdvice 
   implements ThrowsAdvice
{
    public void afterThrowing(Method method, 
                            Object[] args, 
                            Object target, 
                            Throwable subclass)
    {
       System.out.println(
         "Logging that a " + 
         subclass + 
         "Exception was thrown.");
    }
}

The final step in applying the Logging aspect is to amend the springconfig.xml configuration to incorporate the additional new LoggingThrowsAdvice advice.

Figure 3 shows the UML sequence diagram for when the MainApplication is run and the Logging aspect has been applied using the Spring framework.

Click for larger view
Figure 3. Sequence diagram once the Logging aspect is applied to the BusinessLogic bean (click for full-size image)

The Logging aspect shown here effectively demonstrates how to reuse parts of existing aspects and how to use the throws form of advice within the Spring framework. More complex logging can be achieved by declaring new advice for the before and after advice to override the existing Method Tracing aspect implementation, in order to log to a more complex logging framework such as LOG4J. For the source code for the Logging aspect and example application see the references section at the end of this article.

Conclusion

This article has shown some simple aspects being applied using the fundamental AOP constructs available from the Spring framework. In the next part of this series, we'll get into some more practical aspects, exploring aspect lifecycles, using the Spring framework's around advice, and apply AOP patterns using Spring.

References

Russell Miles is a senior consultant for SpringSource in the UK and contributes to various open source projects while working on books for O'Reilly.


Return to ONJava.com.