Spring AOP with AspectJ: boot time

If I use AspectJ based on Spring AOP, then I am attached to setting my aspects to use load time weaving? Or does Spring AOP also support run / compile times while using the AspectJ-based approach?

+5
source share
2 answers

Spring AOP is proxy based. Unless otherwise noted, Spring AOP performs runtime driving.

Weaving: binding aspects to other types of applications or objects to create a recommended object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at run time. Spring AOP, like other pure Java AOP frameworks, weaves at runtime.

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


However, you can configure Spring to do the loading in time. Check out the 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 use @EnableLoadTimeWeaving in your Java Config class. The setup is pretty straightforward and your @Aspect classes @Aspect not change.

Developers simply modify one or more files that form the application context to enable weaving at boot time, rather than relying on administrators who are usually responsible for deploying the configuration, such as running a script

+4
source

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

  • As singh101 says, Spring AOP is based on a proxy server, more specifically based on dynamic Java SE proxies (for interfaces) or CGLIB proxies (for classes). It uses a subset of AspectJ syntax and represents a kind of "AOP lite" approach, which is mainly limited to method call points, the absence of many AspectJ pointcut types, such as method calls, set / get of a member of the class, invocation / execution of the constructor and others. Technologically, it is very different from AspectJ and always carries overhead at runtime due to the proxy approach (a call to indirection). In addition, it is limited to Spring Bean methods that are called from outside the Bean class, that is, it does not work if the Bean calls one of its own methods (because it does not go through the corresponding proxy) and it also does not work for Spring Bean classes (normal POJOs )
  • AspectJ, on the other hand, is a full-fledged AOP framework that does not rely on either proxies or the Spring framework. However, it can be easily incorporated into Spring applications. It works by generating byte code directly through its own compiler (which is a superset of the Java compiler) or tooling of existing byte code. AspectJ can be used at compile time (no overhead at runtime) or while loading classes (load time over time, LTW). While LTW has little overhead at application startup (but the same applies to Spring AOP), both AspeJ weaving approaches do not have overhead at run time due to call indirection, since proxies are not involved.
  • The Spring AOP chapter of the tutorial perfectly explains how to integrate full AspectJ into Spring when Spring AOP is not powerful enough or just too slow.
+10
source

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


All Articles