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:

Next: Direct Web Remoting with DWR and Spring