Search code examples
spring-bootaspectj

Is aspectjweaver dependency mandatory for Spring Boot?


From time to time, I validate the transitive dependencies packaged into my final artifact. I was quite surprised by aspectjweaver (2 MB) since I do not configure it myself. I see how it is introduced:

+- org.springframework.boot:spring-boot-starter-data-jpa:jar:3.2.1:compile
|  +- org.springframework.boot:spring-boot-starter-aop:jar:3.2.1:compile
|  |  \- org.aspectj:aspectjweaver:jar:1.9.21:compile

So my question is: Is this dependency mandatory or is it safe to exclude it? Or does Spring Boot rely on it?


Solution

  • This is actually an interesting question. It deserves an answer with some background information.

    M. Deinum wrote:

    It is mandatory as it is used for AOP to scan and read pointcuts.

    The answer is correct. I want to explain why:

    1. Spring AOP needs to parse and match pointcuts such as execution(* *(..)) or within(my.app..*). For that, the smaller (~580 KB) aspectjmatcher would be enough. The matcher is a subset of the aspectjweaver (~2.0 MB).

    2. Spring AOP also supports annotation-style aspect definitions in AspectJ (sub-)syntax. For annotations like @Aspect, @Before, @Around, @Pointcut, it needs the even smaller (~125 KB) runtime aspectjrt. The runtime is also a subset of the aspectjweaver.

    3. Unfortunately, the matcher does not contain the runtime annotations, because they are not necessary for parsing pointcuts. I.e., the matcher alone is not enough.

    4. We also cannot simply exclude the weaver and use the smaller pair of matcher + runtime instead (I tried!), because Spring actually uses weaver classes for its own proxy-based "AOP lite" approach. Those classes are neither present in the runtime nor in the matcher. I.e., we really do need the weaver.

    5. The weaver really is a superset of both runtime and matcher, which on top also contains the capability to instrument byte code for native AspectJ load-time weaving, if the user decides that Spring AOP is not powerful enough. I.e., because of the weaver being a necessary dependency for Spring anyway, we also get native AspectJ LTW for free, in case we want to use spring-aspects, our own or any other third-party native aspects.

    6. Somewhat off-topic, there is an even bigger (~15 MB) artifact aspectjtools, which is only necessary if we want to use the AspectJ compiler for compile-time or post-compile weaving, should the need arise to save the LTW start-up overhead and bake the aspects right into the class files. I.e., this dependency is not needed for Spring AOP or AspectJ LTW and therefore optional.