23

Can someone explain AOP concepts for dummies: join point, point cut, weaving etc.

For example: Aspect: a modularization of a concern that cuts across multiple classes. What does that mean?

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
dumbJoe
  • 239
  • 2
  • 3

2 Answers2

17

Think of a concern as a functionality group e.g. logging, auditing, security

These functionalities are ever present in most code, but they don't really live in our animal->dog classes - they are functionality that should live in many classes - they are cross cutting concerns.

  • A Joinpoint is a place in the code where the aspect code is actually ran.

  • A pointcut is how to say what code runs at the jointpoint.

  • Weaving - is when the compiler/system takes your normal code and includes all the AOP code so that it triggers the correct code etc - can think of this as an extra pre-compile step.

A simple understandable example is:

  1. We want to track all method calls for debugging purposes (i.e. We want to see which methods get called in what order).
  2. We create an aspect that logs the name of the method when it gets called
  3. We create pointcuts that associate the aspect with all methods in the code. In most AOP frameworks you can do this easily with some wild cards
  4. We now have logging of every method ever called.

Please be aware that terminology differs slightly between different implementations and AOP frameworks.

Martijn Verburg
  • 22,006
  • 1
  • 49
  • 81
Jonno
  • 1,738
  • 10
  • 17
  • Just as I was writing my lengthy answer! Yours is more concise, I like it. Will edit it slighty for clarity. – Martijn Verburg Aug 08 '11 at 16:09
  • Thanks, I should learn the formatting rules, keep forgetting. – Jonno Aug 08 '11 at 16:15
  • Seems like this would be simple to implement with Python decorators or Lisp macros- Y/N ? – Paul Nathan Aug 08 '11 at 16:39
  • @Paul - not familiar with either construct you mention I'm afraid and a quick google didn't give me enough insight to answer either way. – Jonno Aug 08 '11 at 17:25
  • @PaulNathan: AOP is pretty simple in dynamic languages. A simple AOP framework can be created in an afternoon. In compiled languages like Java life is not so sweet. – kevin cline Oct 14 '14 at 17:06
  • Weaving does not sound entirely correct, especially the part where you say: 'can think of this as an extra pre-compile step'. – Koray Tugay Oct 28 '17 at 18:17
  • @KorayTugay it depends on the system/framework, some are pre-compile, others are different :-) – Jonno Nov 07 '17 at 18:09
1

Read in sequence

Each definition builds on the previous ones.

Cross cutting concern

Something that bothers you (concern) everywhere (cross-cutting) across you application. e.g logging or security

Aspect

A chunk of code that is automatically added before, after, or both (around) your existing code

PointCut & JoinPoint

                  Application code
                           |
                           |
                           |
                           |
                          \|/
                           |
JoinPoint  ----------->("before" aspect, on the way in)  // just before your intercepted application code
Pointcut start--------> public String myMethod(params ...)
                   //your application code
Pointcut finish--------> return (foo)
JoinPoint  ----------->("after" aspect, on the way out)  // just after your intercepted application code
                           |
                          \|/
                           |
                           |
                           |
                  Application code

Pointcut

A place in code execution where part of the aspect execute.

JoinPoint

The point where your aspect "joins" the pointcut. At a "before" joinPoint you will have access to parameters, at "after" joinPoint you will have access to the return value. If an exception is thrown, you may need a "try-catch-finally" to handle or re-throw it.

Weaving

The process of adding aspects around a pointcut

Prashant
  • 119
  • 4
  • Whoever voted my answer down, please leave a comment why - if you don't mind. I promise I will not start an argument over it. I would, however like to know what I could have done better. Thank you! – Prashant Apr 06 '19 at 16:58