16

Say Service calls Dao class on which logging aspect(annotational) needs to be applied. I am wondering how aspects actually gets applied.

As per my understanding at the time of DAO injection under Service object, spring finds out that there is some aspect(in this case logging) is configured for DAO, so it injects the proxy object instead of actual target object. Now when actual call is made to any method inside DAO, proxy applies the aspects and then call the actual target object. Is that correct ? Also i believe this is called Run time weaving.

On the other hand same can be done with load time weaving(with javaagent configuration) where byte code manipulation is done for classes on which aspects needs to be applied. So proxy does not come into picture here.

Please correct me if i am wrong as this is the foundation for all spring modules?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
M Sach
  • 33,416
  • 76
  • 221
  • 314

2 Answers2

22

Your understanding is right. Spring AOP is proxy-based. Spring uses either JDK proxies (preferred wheneven the proxied target implements at least one interface) or CGLIB proxies (if the target object does not implement any interfaces) to create the proxy for a given target bean.

Unless configured to do otherwise, Spring AOP performs run-time weaving. You can however set up Spring to do load-time weaving through AspectJ. Check the documentation link for more details.

Hung Nguyen
  • 5,458
  • 1
  • 10
  • 8
Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113
  • i believe load time weaving is better in terms of performance as there would not be performance overhead in deciding whether proxy needs to be injected based on aspect configuration which is the case for run time weaving. Yes in load time weaving server startup will take bit longer but thats fine as its one time activity – M Sach Dec 20 '14 at 16:48
  • The performance could be better in load time weaving (or more specifically compile time weaving).Best would be to verify it actually through a load test) but there are other areas you might want to consider like the changes in the build system. You would have include this step before your artifacts are generated. – Andy Dufresne Dec 21 '14 at 03:22
  • 3
    Just an useful quotation - "*All problems in computer science can be solved by another level of indirection, except of course for the problem of too many indirections.*" – David Wheeler – smwikipedia Oct 15 '15 at 06:47
  • 1
    great explanation. two Qs -> first: can we say Spring-AOP uses reflection but aspectJ doesnt ? second: isnt using reflection expensive from the point of efficiency ? – Amir Ziarati Jun 06 '18 at 08:21
  • 1
    @AmirZiarati I will try to respond: 1.Both are using reflection, but AspectJ "uses it at minimum" (see https://www.eclipse.org/aspectj/doc/released/faq.php#q:reflection) 2. Yes, reflection is expensive and should be used with care. – Victor Jan 03 '19 at 14:19
  • 2
    @AndyDufresne Your link to Spring AOP internals is not functional. – improbable Oct 04 '19 at 10:45
4

Still there are two points to clarify here

First one in my post is actually load time weaving not run time weaving

From this link

Load-time weaving (LTW) is simply binary weaving defered until the point that a class loader loads a class file and defines the class to the JVM. To support this, one or more "weaving class loaders", either provided explicitly by the run-time environment or enabled through a "weaving agent" are required.

Second one is compile time weaving

Compile-time weaving is the simplest approach. When you have the source code for an application, ajc will compile from source and produce woven class files as output. The invocation of the weaver is integral to the ajc compilation process. The aspects themselves may be in source or binary form. If the aspects are required for the affected classes to compile, then you must weave at compile-time. Aspects are required, e.g., when they add members to a class and other classes being compiled reference the added members.

M Sach
  • 33,416
  • 76
  • 221
  • 314