Showing posts with label aop. Show all posts
Showing posts with label aop. Show all posts

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.

Monday, January 2, 2012

AspectJ vs Javassist

AspectJ:

Advantages:
  • Very powerful, can do nearly anything you might need
  • Powerful pointcut expressions for defining where to inject an advice and when to activate it (including some run-time checks) – fully enables DRY, i.e. write once & inject many times
  • Both build-time and load-time code injection (weaving)
Disadvantages:
  • The modified code depends on the AspectJ runtime library
  • The pointcut expressions are very powerful but it might be difficult to get them right and there isn’t much support for “debugging” them though the AJDT plugin is partially able to visualize their effects
  • It will likely take some time to get started though the basic usage is pretty simple (using @Aspect, @Around, and a simple pointcut expression, as we will see in the example)

Javassist:

Advantages:
  • No run-time dependencies if build-time weaving used (load-time weaving requires the GluonJ agent library or gluonj.jar)
  • Simple Java syntax using GlutonJ’s annotation – though the custom syntax is also trivial to understand and easy to use
  • Easy, automatic weaving into the target classes with GlutonJ’s JAR tool, an Ant task or dynamically at the load-time
  • Support for both build-time and load-time weaving
Disadvantages:
  • An aspect can modify only a single class, you cannot inject the same piece of code to multiple classes/methods
  • Limited power – only provides for field/method addition and execution of a code instead of/around a target method, either upon any of its executions or only if the execution happens in a particular context, i.e. when called from a particular class/method