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.

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
- Source code for this article
- The Spring Java/J2EE Framework
- The Spring AOP Framework
- "Introduction to Aspect-Oriented Programming"
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.
-
Automatic Swap of Target Class
2006-03-29 22:54:31 spidercat [View]
-
Automatic Swap of Target Class
2006-03-30 00:53:13 Russell Miles |
[View]
-
Automatic Swap of Target Class
2006-03-30 19:04:31 spidercat [View]
-
Automatic Swap of Target Class
2006-03-31 00:06:31 Russell Miles |
[View]
-
Automatic Swap of Target Class
2006-04-06 01:57:53 spidercat [View]
- Trackback from http://www.cnblogs.com/kavenmo/archive/2004/10/29/58163.html
An Introduction to Aspect-Oriented Programming with the Spring Framework
2004-10-29 01:24:43 [View]
- Trackback from http://ict.tippinst.ie/~jhannafin/www/2004/10/13.html#a378
An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1
2004-10-13 07:53:24 [View]
-
The diagrams are wrong?
2004-08-19 04:44:10 patrickchaumette [View]
-
The diagrams are wrong?
2004-08-19 21:24:37 Russell Miles |
[View]
- Trackback from http://sbserve/DotText/simon/archive/2004/07/22/1976.aspx
An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1
2004-07-21 14:38:30 [View]
- Trackback from http://www.ebackend.com/infoagg/archives/2004_07.html#001741
An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1
2004-07-15 11:11:54 [View]