2

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.

Sebastian Götz
  • 459
  • 7
  • 15
  • Assuming you have some dependency management in place (maven, gradle) could you add the dependencies from that to your question as well? Another thing that would be interesting is to enable debug logging for `org.springframework.context.weaving` and `org.springframework.instrument`. Finally check if your tomcat uses a javaagent for loadtimeweaving (if so you need to upgrade that as well!). – M. Deinum Feb 11 '21 at 09:59
  • You could try the `5.2.0.M1`, `5.2.0.M2` etc. up to the release, to see where it stopped working and as mentioned enable the aforementioned loggers to get more information of what is different. A final thought make sure there are no Spring libraries (or the agent!) active in some shared directory for Tomcat. – M. Deinum Feb 11 '21 at 13:26
  • AspectJ 1.9.2+ should support Java 11, the current version 1.9.6 supports up to Java 14. Aside from that, I am not a Spring or Tomcat buff, I just have a little Spring playground project using Boot, just in case I want to answer Spring AOP questions or AspectJ LTW in used in Spring questions like yours. Usually I configure Spring via annotations, though. I can take a look, if you could condense your problem into an [MCVE](https://stackoverflow.com/help/mcve) and post that on GitHub. I like to have something reproducible. Just a mini application with one aspect or so. – kriegaex Feb 12 '21 at 01:56
  • Appears to be a known [issue](https://github.com/spring-projects/spring-framework/issues/26199) in the spring tracking system. – Sebastian Götz Feb 12 '21 at 06:15

1 Answers1

0

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

Sebastian Götz
  • 459
  • 7
  • 15