Spring AOP Basics
AOP, Aspect Oriented Programming, decomposes a system into concerns, instead of objects. AOP attempts to deal with "aspects" that cross-cut across the code of a system. This cross-cutting code can be difficult or impossible to modularize with OOP. The most common example of a cross-cutting concern is logging. Code for doing logging typically must be scattered all over a system. With AOP, you can declare, for example, that a system should write a log record at the beginning and end of all method invocations.
Probably the most popular framework for AOP is AspectJ. Alternatively, Spring provides AOP services that implement the AOP Alliance interfaces. Spring also provides hooks to integrate with AspectJ, but I will not discuss that here.
In Spring, typically you configure AOP in your BeanFactory XML files. Basically you just wire in services to intercept or filter calls to the core services that are wired into your BeanFactory.
Spring provides both:
- Pre-packaged AOP services for:
- declarative transaction management
- security
- logging
- An infrastructure for custom services such as:
- Auditing
- Caching
- Logging
Here, I will focus on writing a simple aspect to provide a caching service. Spring now has some built-in capability for AOP caching services. Spring's built-in caching infrastructure would be preferred for production use. This is just an example to illustrate how Spring AOP works.
TODO: short version of my AOP article.
Further Reading:
- J2EE Without EJB - Chapter 8 - Declarative Middleware Using AOP Concepts by Rod Johnson and Juergen Holler. This chapter has a nice "AOP 101" section that is a good intro to the subject.
- AspectJ in Action by Ramnivas Laddad - This books is an EXCELLENT read on AOP in general.
- An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1 and Part 2
- Spring AOP Framework Exploration
- My article on Spring AOP - here I do the same example as I did here, but I pretend to naively "arrive" upon using Spring AOP.





