Chapter 11. AOP

Aspect-oriented programming (AOP) is a powerful technique to simplify software by identifying “cross-cutting concerns,” or functionality that cannot be cleanly modularized into a single component, class, or module, and applying that logic centrally rather than scattering copies across the code base. In a Grails application, this might involve recognizing that security checks (or logging, timing, or any other shared logic) are required in services, controllers, and Quartz jobs, for example; although the purpose of each of these artifacts is quite different, they share a common need that “cuts across” the different types. There are several simplistic approaches that could be used, but they involve code duplication, artificial class hierarchies, or other brittle approaches that make the code harder to maintain. But using AOP would more likely be a much cleaner and more maintainable approach, because there isn’t a proper object-oriented approach that can solve the problem.

AOP is a large topic, but for the sake of this discussion, let’s focus on before, after, around, and after-throwing interception, which is useful to separate what gets executed from when it gets executed. before interception is where code runs before one or more specified methods, and potentially blocks access to the method by throwing an exception (e.g., in a security check where it’s determined that you’re not allowed) or does some work before the method runs (e.g., starting or joining a transaction). ...

Get Programming Grails now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.