18

What's the advantage and disadvantage of these two type of AOP framework? I'm using Unity as my aop framework, but I guess the compile-time aop framework such as postsharp may have better performance than run-time aop framework? It looks like run-time aop framework use reflection to implement the injection.

1 Answer 1

62

I am not a .NET guy, but I know the AOP world in the Java ecosystem, especially AspectJ and Spring AOP. Basically there are 4 types of aspect weaving:

  • Source code weaving: Aspect code is injected as source code statements into your application source code. This is some kind of preprocessor approach. No AOP framework in the Java world uses this approach nowadays, but there used to be some in the early days of AOP.
    • The advantage would be complete independence of any runtime libaries or special AOP compilers during runtime, if done right.
    • The disadvantage would be bloated source code and a preprocessing / code generation step before compilation. You would always need the generated source code for debugging.
  • Compile-time weaving: Aspect code is woven into your application by a special compiler.
    • The advantage is no runtime overhead for aspect weaving. The only thing you need is a small runtime library on your classpath.
    • The disadvantage is that you cannot defer the decision if you want to weave aspects into your application at all to runtime. But this is only a problem when dealing with debugging or tracing aspects not needed all the time. Another disadvantage is that this approach only works for code under your control, i.e. you need to have the source code. It does not work for 3rd party libs.
  • Binary weaving: Aspect code is woven into existing class files after compilation rather than during compilation.
    • The advantage is that it also works for 3rd party code you do not have the source code for. This approach can also be mixed with compile-time weaving. You also avoid the overhead of load-time weaving (see below).
    • The disadvantages are similar to compile-time weaving: You cannot unapply an aspect once it is woven into the code, merely deactivate its execution by pointcuts such as if(). But this can be quite efficient.
  • Load-time weaving (LTW): A weaving agent/library is loaded early when your VM/container is started. It gets a configuration file with rules describing which aspects should be woven into which classes.
    • The advantage is that you can dynamically decide if/what to weave. If done via byte-code transformation and not via dynamic proxies or reflection (see below), the resulting bytecode is equally efficient as the one created via compile-time or binary weaving. Another advantage is that like binary weaving it works for your own code as well as 3rd party code, as long as the weaving agent can "see" it, i.e. it happens in a child classloader.
    • The disadvantage is the one-time weaving overhead during application start-up because weaving is done while classloading occurs.
  • Proxy-based LTW: This special LTW form is used by Spring AOP while AspectJ does the previous 3 forms listed above. It works by creating dynamic proxies (i.e. subclasses or interface implementations) for aspect targets.
    • I cannot think of any special advantage other than maybe your framework of choice (such as Spring) happens to support it.
    • Disadvantages are limitation to public, non-static methods and runtime overhead due to the proxy-based approach. It also does not capture internal method calls, i.e. when a proxied class calls one of its own methods because those calls are not captured by the proxy. Special types of pointcuts such as constructor interception, member variable read/write access and many more are not supported, making this more of an "AOP lite" approach. But it can be sufficient for your purposes.

Generally, good aspect compilers such as AspectJ create very efficient bytecode and do not heavily rely on reflection during runtime. If your aspect framework of choice does rely on reflection, probably it is not very fast. But maybe it is fast enough, depending on how heavily you use aspects.

Probably I have written too much already, but I could write even more. This is why I am stopping now. Besides, this kind of question is not well-suited to StackOverflow because it can lead to philosophical discussions and opinion-based debates. I hope I managed to be fairly objective/impartial even so.

7
  • that was awesome explanation. cant we say aspectJ doesnt use reflection ? also you said AspectJ does these three : compile-time, binary, load-time can we choose which one to use or it happens internally ? Commented Jun 6, 2018 at 8:35
  • Amir, you can learn more about AspectJ by reading the documentation. I suggest to try and use it instead of asking yourself theoretical questions about it. As for the second question, you as a developer decide which type of weaving you want to use. AspectJ does not do anything without you configuring your build environment correspondingly.
    – kriegaex
    Commented Jun 10, 2018 at 4:30
  • @kriegaex Does aspectj compiler has any disadvantage over java compiler? Let's say we use compile time weaving and we face an issue due to the aspectj compiler. In the case where we can't get the aspectj compiler fixed, we'll have to make a lot of changes to get things working again. In short, compile time weaving seems like the best choice but aspectj compiler seems like a deal breaker. What is your opinion on that?
    – hrzafer
    Commented Sep 9, 2018 at 23:05
  • My opinion is: Some people find a solution for every problem. Others find a problem in every solution. Besides, the AspectJ compiler is a regularly refreshed fork of the Eclipse Java compiler and very well maintained. It is much easier to get the maintainer's attention than with the Oracle JVM. My recommendation is: See chances too, not just risks. I cannot see any "deal breaker" here.
    – kriegaex
    Commented Sep 10, 2018 at 17:26
  • Really appreciate your answer!
    – The Tran
    Commented May 21, 2019 at 2:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.