Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Spring AOP, does spring creates proxy object every time a target bean is created or it happens only first time?

Does spring creates proxy object every time a target bean is created or it happens only the first time in Spring AOP?

like image 511
Vikrant Chaudhary Avatar asked Dec 19 '22 03:12

Vikrant Chaudhary


1 Answers

Weaving

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.

Types of Weaving

1. Source 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

2. Binary Weaving

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

3. Load Weaving

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.

4. Runtime Weaving

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.

Questions

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.

  • During application startup, the Spring framework instantiates the target bean, BookController, and creates a proxy of the target class. The proxy of the target class is created only once.
  • If the target class doesn't implement any interface, the proxy is of type CglibAopProxy. Otherwise, the proxy is of type JdkDynamicAopProxy.
  • Every time the 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)

like image 167
Indra Basak Avatar answered Dec 28 '22 08:12

Indra Basak