I am using Spring’s declarative transactions (the @Transactional annotation) in “aspectj” mode. It works in most cases exactly like it should, but for one it doesn’t. We can call it Lang (because that’s what it’s actually called).
I have been able to pinpoint the problem to the load time weaver. By turning on debug and verbose logging in aop.xml, it lists all classes being woven. The problematic class Lang is indeed not mentioned in the logs at all.
Then I put a breakpoint at the top of Lang, causing Eclipse to suspend the thread when the Lang class is loaded. This breakpoint is hit while the LTW weaving other classes! So I am guessing it either tries to weave Lang and fails and doesn’t output that, or some other class has a reference that forces it to load Lang before it actually gets a chance to weave it.
I am unsure however how to continue to debug this, since I am not able to reproduce it in smaller scale. Any suggestions on how to go on?
Update: Other clues are also welcome. For example, how does the LTW actually work? There appears to be a lot of magic happening. Are there any options to get even more debug output from the LTW? I currently have:
<weaver options="-XnoInline -Xreweavable -verbose -debug -showWeaveInfo">
I forgot tom mention it before: spring-agent is being used to allow LTW, i.e., the InstrumentationLoadTimeWeaver.
Based on the suggestions of Andy Clement I decided to inspect whether the AspectJ transformer is ever even passed the class. I put a breakpoint in ClassPreProcessorAgent.transform(..), and it seems that the Lang class never even reaches that method, despite it being loaded by the same class loader as other classes (an instance of Jetty’s WebAppClassLoader).
I then went on to put a breakpoint in InstrumentationLoadTimeWeaver$FilteringClassFileTransformer.transform(..). Not even that one is hit for Lang. And I believe that method should be invoked for all loaded classes, regardless of what class loader they are using. This is starting to look like:
- A problem with my debugging. Possibly
Langis not loaded at the time when Eclipse reports it is - Java bug? Far-fetched, but I suppose it does happen.
Next clue: I turned on -verbose:class and it appears as if Lang is being loaded prematurely – probably before the transformer is added to Instrumentation. Oddly, my Eclipse breakpoint does not catch this loading.
This means that Spring is new suspect. there appears to be some processing in ConfigurationClassPostProcessor that loads classes to inspect them. This could be related to my problem.
These lines in ConfigurationClassBeanDefinitionReader causes the Lang class to be read:
else if (metadata.isAnnotated(Component.class.getName()) ||
metadata.hasAnnotatedMethods(Bean.class.getName())) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
return true;
}
In particular, metadata.hasAnnotatedMethods() calls getDeclaredMethods() on the class, which loads all parameter classes of all methods in that class. I am guessing that this might not be the end of the problem though, because I think the classes are supposed to be unloaded. Could the JVM be caching the class instance for unknowable reasons?
OK, I have solved the problem. Essentially, it is a Spring problem in conjunction with some custom extensions. If anyone comes across something similar, I will try to explain step by step what is happening.
First of all, we have a custom
BeanDefintionParserin our project. This class had the following definition:Now, the problem occurs after all bean definition have been read and
BeanDefinitionRegistryPostProcessorbegins to kick in. At this stage, a class calledConfigurationClassPostProcessorstarts looking through all bean definitions, to search for bean classes annotated with@Configurationor that have methods with@Bean.In the process of reading annotations for a bean, it uses the
AnnotationMetadatainterface. For most regular beans, a subclass calledAnnotationMetadataVisitoris used. However, when parsing the bean definitions, if you have overriden thegetBeanClass()method to return a class instance, like we had, instead aStandardAnnotationMetadatainstance is used. WhenStandardAnnotationMetadata.hasAnnotatedMethods(..)is invoked, it callsClass.getDeclaredMethods(), which in turn causes the class loader to load all classes used as parameters in that class. Classes loaded this way are not correctly unloaded, and thus never weaved, since this happens before the AspectJ transformer registered.Now, my problem was that I had a class like so:
Then, I had a bean of class
Somethingthat was parsed using our customControllerBeanDefinitionParser. This triggered the wrong annotation detection procedure, which triggered unexpected class loading, which meant that AspectJ never got a chance to weaveLang.The solution was to not override
getBeanClass(..), but instead overridegetBeanClassName(..), which according to the documentation is preferable:Lesson of the day: Do not override
getBeanClassunless you really mean it. Actually, don’t try to write your own BeanDefinitionParser unless you know what you’re doing.Fin.