springaspectjload-time-weaving

Spring AspectJ Load Time Weaving not working with 5.3.3 and Tomcat 9.0.37


I am involved in a review task of an older project. The task is to update certain libraries to more recent versions. This project successfully used load-time-weaving with spring (4.3.14.RELEASE) together with AspectJ (1.9.0) and Tomcat 8.0.20 under JDK 8 with. Now spring shall be updated to the most recent version (5.3.3 at the moment) and also the Tomcat version shall be lifted to a recent version (9.0.37 targeted for the moment). The server shall be running under JDK 11. After upgrading the libraries, we recognized that AspectJ was not working anymore. So I started to debug into this. AspectJ is activated from an XML configuration like this:

<context:load-time-weaver />

Debugging the startup of the container a stumbled over this piece of code in org.springframework.context.config.LoadTimeWeaverBeanDefinitionParser:

protected boolean isAspectJWeavingEnabled(String value, ParserContext parserContext) {
    if ("on".equals(value)) {
        return true;
    }
    else if ("off".equals(value)) {
        return false;
    }
    else {
        // Determine default...
        ClassLoader cl = parserContext.getReaderContext().getBeanClassLoader();
        return (cl != null && cl.getResource(AspectJWeavingEnabler.ASPECTJ_AOP_XML_RESOURCE) != null);
    }
}

As we do not provide any attribute to the XML tag, AspectJ is in auto-detect mode causing the code in the else branch to be executed. There the reference to the classloader is null, leading to AspectJ being disabled.

An attempt to explicitly activate AspectJ by passing <context:load-time-weaver aspectj-weaving="on"/> had no effect in the end. AspectJ was set active and the definitions were loaded but none of the Aspect definitions (META-INF/aop.xml) were detected applied. As we have changed nothing so far by means of the AspectJ functionality or package structure, something must have changed in spring from 4.3.14 to 5.3.3) or AspectJ (1.9.0 to 1.9.4). A quick view in the GitHub repo showed my only one significant change. But debugging this, the classloader used before the change was also null.

Has anyone had similar problems getting AspectJ to work this way? It looks to me that the problem is detecting the aop.xml files on the classpath.

EDIT: I have made some more research using different combinations of JDK and Tomcat versions. The problem is not related to those two. When upgrading spring version by version, I found out that it worked fine till 5.1.20.RELEASE. Starting with 5.2.0.RELEASE my problems start. Meanwhile I have AspectJ logging active so I can see that some classes are woven but the majority of those classes I expect being woven, are not.


Solution

  • This was caused by a regression in spring framework since 5.2.0.RELEASE. Here is the issue for that.