Topic 6
■■■
Introducing Spring AOP
Besides Dependency Injection (DI), another core feature that the Spring Framework brings to the
developer community is Aspect-Oriented Programming (AOP). Although it used to be difficult to learn,
understand, and implement, thanks to Spring's intensive use of AOP within the framework and a
simplified AOP programming model that Spring provides, AOP has become a technique that developers
use on day-to-day development, especially when developing Spring-based applications. AOP is often
referred to as a tool for implementing crosscutting concerns. When you "cut through" the unfamiliar
terminology, 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
look at 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 this section.
Types of AOP: There are two distinct types of AOP: static and dynamic. In static AOP,
·
like that provided by AspectJ's (http://eclipse.org/aspectj/) compile-time weaving
mechanisms, 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 crosscutting 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.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home