Saturday, November 17, 2018

AspectJ tutorials


AspectJ and AOP – The black magic of programming


https://blog.jayway.com/2015/09/03/aspectj-and-aop-the-black-magic-of-programming/

AspectJ – Dictionary

Aspects

The easiest way to describe aspects is as a funky Java Class. An Aspect contains other things than a normal class such as; pointcuts, advice, advice bodies and inner-type declarations. An aspect may also contain regular java classes and methods.

Pointcuts

Defines, in a multitude of different ways, a point in the code. The pointcut defines when an advice should be run.

Advice / Advice Body

Similar to a java method; contains the code that will be run once a pointcut has been triggered.

Annotation – Not AOP specific

Consists of meta-data and can be used at methods, classes, parameters, packages and in variables.
Annotations can contain an optional list of element-value pairs, such as ‘yourProperty = “someValue”‘ in the example above. In AspectJ we can define a pointcut by looking for annotations. The pointcut and advice can then use the element-value pairs from the annotation.

Weaving / Aspect weaving

There are a few different ways to inject the AOP code in our application, but one common denominator is that they all require some type of extra step to be applied to our code. This extra step is called weaving.

Compile-time weaving

If you have both the source code of the aspect and the code that you are using aspects in, you can compile your source-code and the aspect directly with an AspectJ compiler.

Post-compile weaving / Binary weaving

If you can’t, or don’t want to use source-code transforms to weave the aspects into the code, you can take already compiled classes or jars and inject aspects.

Load-time weaving

Acts the same way as post-compile weaving / binary weaving but waits to inject aspects into the code until the class loader loads the class file. This requires one or more weaving class loaders.

No comments: