Does spring creates proxy object every time a target bean is created or it happens only the first time in Spring AOP
?
Weaving is a technique of interweaving target classes with aspects so that an advice gets executed when a suitable condition arises.
AspectJ provides three types of weaving mechanisms, namely: source weaving, binary weaving, and load weaving.
While Spring AOP only offers runtime weaving.
The source weaving is part of the compilation process where the Java source code (*.java
) is compiled with AspectJ compiler (ajc
) into woven bytecode (*.class
). Here, ajc
replaces javac
(Java compiler).
Target.java + MyAspect.java -- (compiled with ajc) --> Target.class
In binary weaving, the target and aspect source code (*.java
) are compiled separately into binary classes (.class
). The binary classes are then woven together with AspectJ compiler (ajc
).
STEP 1: Compile Target class
Target.java -- (compiled with javac) --> Target.class
STEP 2: Compile Aspect class
MyAspect.java -- (compiled with javac or ajc) --> MyAspect.class
STEP 3. Aspect Weaving
Target.class + MyAspect.class -- (woven with ajc) --> Target.class
In load weaving, the binary target and aspect classes (*.class
) are modified (instrumented
) during the loading of the classes by JVM with technologies such as javaagent
.
Due to inherent proxy-based nature of the Spring framework, the Spring AOP only offers runtime weaving. This means targeted class instances are changed into proxies during application startup ( or any other time during runtime before its used). Any calls to targeted methods are intercepted accordingly by the target proxy classes to execute any suitable advice.
Does Spring creates proxy object every time a target bean is created or it happens only the first time in Spring AOP?
Let's say you have created an aspect on a method named create
of a controller named BookController
.
BookController
, and creates a proxy of the target class. The proxy of the target class is created only once.CglibAopProxy
. Otherwise, the proxy is of type JdkDynamicAopProxy
.create
method of the target class, BookController
, is invoked, the Spring framework wraps the original proxy inside a join point (MethodInvocationProceedingJoinPoint
in this case) before passing it on to your aspect.Spring AOP supports run-time weaving only, does that mean the target proxy gets created only when the target method is called for the first time?
When Spring AOP states runtime, it is trying to differentiate between other types of weavings in AspectJ. A Spring runtime can mean anytime while a Spring application is running.
As to the question when exactly does a proxy gets created, it can be created lazily or proactively. In most cases, the target classes are proxied during a Spring application startup.
In the above example, the controller instance is turned into a proxy even before any method on the controller is invoked, i.e., before the bottom line is logged in the console.
2017-11-07 20:12:27.541 INFO 31394 --- [ main] com.basaki.Application : Started Application in 147.808 seconds (JVM running for 149.184)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With