How do spring aspects work inside?

Say Service calls the Dao class on which the logging attribute (annotation) should be applied. I am wondering how aspects are actually applied.

According to my understanding, during the DAO injection, under the spring service object, it turns out that there are some aspects (in this case, logging) configured for the DAO, so it introduces a proxy object instead of the real target. Now that the actual call is made in some way inside the DAO, the proxy applies the aspects and then calls the actual target of the object. It's right? In addition, I believe this is called Run time weaving.

On the other hand, the same can be done using the loading time trend (with javaagent configuration), where byte code manipulation is performed for classes for which aspects should be applied. Thus, the proxy does not appear here.

Please correct me if I am wrong, as this is the basis for all spring modules?

+6
source share
2 answers

Your understanding is correct. Spring AOP is proxy based. Spring uses either JDK proxies (preferably when the proxy server implements at least one interface) or CGLIB proxy (if the target does not implement any interfaces) to create a proxy server for the given bean target.

Unless otherwise noted, Spring AOP performs runtime driving. However, you can configure Spring to do time-loading through AspectJ. See the link for more details.

Link for Spring AOP Proxy

+5
source

There are still two points to clarify here.

The first one in my post is actually the binding time of the time, which does not work during weaving

From this link

Boot time (LTW) is just a binary weave, delayed until the class loader loads the class file and defines the class for the JVM. To support this, one or more “weaving class loaders” are required, either explicitly provided by the runtime environment or enabled by the “weaving agent”.

The second is weaving compilation time

Time compilation is the easiest approach. When you have the source code for the application, ajc will compile from the source and create intertwined class files as output. Calling a weaver is an integral part of the ajc compilation process. Aspects themselves can be in source or binary form. If these aspects are necessary to compile the affected classes, you must interweave at compile time. Aspects are required, for example, when they add members to a class and other compiled classes refer to added elements.

+1
source

Source: https://habr.com/ru/post/979974/


All Articles