An Introduction to Aspect Oriented Programming
by, 11-16-2011 at 04:58 PM (1786 Views)
Aspect-oriented programming (AOP) is a new methodology that provides separation of crosscutting concerns. Crosscutting concerns are system wide concerns that span multiple modules. In applications, these system wide concerns would involve features such as logging, security, persistence and any other element common to a majority of the core business modules. This type of programming is done by introducing a new unit of modularization—an aspect—that crosscuts other modules. Using AOP you would implement crosscutting concerns in aspects instead of integrating them directly into the core modules. You then use an aspect weaver, a compiler-like entity, to compose the final system by combining the crosscutting modules and the core through a process called weaving. The result is that AOP modularizes the cross- cutting concerns in a clear-cut fashion, yielding a system architecture that has a cleaner design that is easier to design, implement, and maintain.
As we are looking at Spring we will focus on Spring’s support of AOP. It has four different flavors to it:
- Pure-POJO aspects
- Injected AspectJ aspects
- Annotation driven aspects using @AspectJ
- Classic Spring proxy-based AOP
With the exception of Injected AspectJ aspects, the rest are variations on Spring’s proxy based AOP. What this means is that Spring’s AOP support is limited to method interception. Injected AspectJ aspects allows you to take advantage of the power of AspectJ in Spring. The nice part of all of this is that irrespective of the flavor that you choose, all the advices (i.e. job of an aspect) are written in Java. We will look at this more in our next tip.