Chapter 6: Introducing Spring AOP


Over the last 18 months, Aspect Oriented Programming (AOP) has become a hot topic in the Java world. Many articles, discussions, and implementations of AOP have become available for Java programmers who want to get into this world. AOP is often referred to as a tool for implementing crosscutting concerns. When you cut through the unfamiliar terminology, this means that you use AOP for modularizing individual pieces of logic, known as concerns, and you apply these concerns to many parts of an application. Logging and security are typical examples of crosscutting concerns that are present in many applications. Consider an application that logs the start and end of every method for debugging purposes. You will probably refactor the logging code into a special class, but you still have to call methods on that class twice per method in your application in order to perform the logging. Using AOP, you can simply specify that you want the methods on your logging class to be invoked before and after each method call in your application.

It is important that you understand that AOP complements OOP, rather than competes with it. OOP is very good at solving a wide variety of problems that we, as programmers, encounter. However, if you take the logging example again, it is quite plain to see that OOP is lacking when it comes to implementing crosscutting logic on a large scale. Using AOP on its own to develop an entire application is practically impossible, given that AOP functions on top of OOP. Likewise, although it is certainly possible to develop entire applications using OOP, you can work smarter by employing AOP to solve certain problems that involve crosscutting logic.

We are going to cover AOP in this chapter and the next. In particular, this chapter covers the following topics:

  • AOP basics: Before we begin discussing Spring's AOP implementation, we cover the basics of AOP as a technology. Most of the concepts covered in this section are not specific to Spring and can be found in any AOP implementation. If you are already familiar with another AOP implementation, then feel free to skip over this section.

  • Types of AOP: There are two distinct types of AOP, static and dynamic. In static AOP, like that provided by AspectJ (, the crosscutting logic is applied to your code at compile time, and you cannot change it without modifying the code and recompiling. With dynamic AOP, like Spring AOP, crosscutting logic is applied dynamically, at runtime. This allows you to make changes in the distribution of cross- cutting without recompiling the application. These types of AOP are complementary and, when used together, they form a powerful combination that you can use in your applications.


    Static and dynamic AOP are distinct from the static and dynamic crosscutting concepts. The differentiation between static and dynamic crosscutting is largely academic and is of no relevance to Spring AOP. For more information on this topic and on AOP as a whole, we recommend that you read AspectJ in Action: Practical Aspect-Oriented Programming by Ramnivas Laddad (Manning, 2003).

  • Spring AOP architecture: In this section, we get down to the nitty-gritty of Spring's AOP implementation. Spring AOP is only a subset of the full AOP feature-set found in other implementations like AspectJ. In this section, we take a high-level look at which features are present in Spring, how they are implemented, and why some features are excluded from the Spring implementation.

  • Proxies in Spring AOP: Proxies are a huge part of how Spring AOP works, and you must understand them to get the most out of Spring AOP. In this section, we look at the two different kinds of proxy: JDK dynamic proxy and CGLIB proxy. In particular, we look at the different scenarios in which Spring uses each proxy, the performance of the two proxy types, and some simple guidelines to follow in your application to get the most from Spring AOP.

  • Using Spring AOP: In this section, we look at some practical examples of AOP usage. We start off with a simple Hello World example to ease you into Spring's AOP code, then we continue with a detailed description of the different AOP features that are available in Spring, complete with examples.

In this chapter, we cover Spring AOP in isolation from much of the rest of the framework. In Chapter 7, we take a much more framework-oriented view of Spring AOP, including how to configure AOP using an ApplicationContext.

Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: