12

If I'm using AspectJ based Spring AOP, am I then tied to configuring my aspects to use load time weaving? Or does Spring AOP also support run time/compile time weaving when using the AspectJ based approach?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Shane
  • 461
  • 2
  • 8
  • 23

2 Answers2

18

I think we should be careful not to mix up Spring AOP vs. AspectJ.

  • Like singh101 said, Spring AOP is proxy-based, more exactly based on Java SE dynamic proxies (for interfaces) or CGLIB proxies (for classes). It uses a subset of AspectJ syntax and is a kind of "AOP lite" approach basically limited to method execution pointcuts, missing many AspectJ pointcut types like method call, class member set/get, constructor call/execution and others. Technologically it is very much different from AspectJ and always incurs a runtime overhead due to the proxy approach (call indirection). Furthermore, it is limited to Spring Bean methods being called from outside the bean class, i.e. it does not work if a bean calls one of its own methods (because it does not go through the corresponding proxy) and it also does not work for non-Spring Bean classes (normal POJOs).
  • AspectJ on the other hand is a full-fledged AOP framework which does not rely on either proxies or the Spring framework. It can be easily included into Spring applications, though. It works by generating byte code directly via its own compiler (which is a superset of the Java compiler) or instrumenting existing byte code. AspectJ can be used during compile time (no runtime overhead) or during classloading (load time weaving, LTW). While LTW has a little overhead during application start-up time (but the same applies to Spring AOP), both AspectJ weaving approaches have no runtime overhead due to call indirection because there are no proxies involved.
  • The Spring manual chapter on AOP explains nicely how to integrate full AspectJ into Spring when Spring AOP is not powerful enough or simply too slow.
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • 1
    Can we use both at once? For example we have old application that use a lot of beans and uses `Spring AOP` with `core-context.xml` file but now we want add one `Aspect` that does not depend on `bean`. Cause of that we make `aop.xml` and use `AspectJ` i.e `load-time-weaving` How we can make sure that our new aspect and `weaving` does not corrupt our old `aspects` ? In this book i see that it's written that we can use both at once: https://books.google.pl/books?id=oMVIzzKjJCcC&lpg=PA168&ots=3EYBuxxJJz&dq=using%20both%20aspectJ%20with%20SPRING%20AOP&pg=PA168#v=onepage&q&f=false – Kamil Witkowski Apr 10 '17 at 12:02
  • 2
    I am not a Spring user. But I think that, if done right, you can combine both. I have not read the book you linked to but if the author says how to do it, I see no reason why it should not work. I think it is just important that pure AspectJ aspects and the beans targeted by them are not picked up twice (e.g. once via Spring component scan and once more via LTW). Cleanly configured, this should not be a problem. Good luck! – kriegaex Apr 10 '17 at 12:19
9

Spring AOP is proxy based. Unless configured to do otherwise, Spring AOP performs run-time weaving.

Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

Source: http://docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/htmlsingle/#aop-introduction-defn


You can however set up Spring to do load-time weaving. Check Spring documentation on how to do this: http://docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw

Among other things, you would be using @EnableLoadTimeWeaving in your Java Config class. The setup is fairly simple and your @Aspect classes would not change.

Developers simply modify one or more files that form the application context to enable load-time weaving instead of relying on administrators who typically are in charge of the deployment configuration such as the launch script

Angad
  • 3,389
  • 3
  • 30
  • 40
  • That's great, thanks. Let me see if I understand; so it sounds like spring aop with aspectj will use run time weaving by default, but can be configured to use load time if desired? – Shane Oct 12 '14 at 15:38
  • 1
    @Angad, is it possible that both run time weaving and load time weaving co-exist? – DecKno Apr 05 '16 at 06:14