In one of our Thymeleaf templates we have the following code:

...
<td th:if="${foobar instanceof T(some.pkg.FooBar)}">Matches</td>
...

This generally works and when foobar is an instance of FooBar the td is shown.

But when I start the application with Dev Tools enabled, and I change something after reloading, the td is no longer shown. After I suspected that class loading may be the issue, I added the following code:

<td th:text="${foobar.getClass().getClassLoader()}"></td>
<td th:text="${T(some.pkg.FooBar).getClassLoader()}"></td>

After each change and reload, this shows that the ClassLoader of the foobar instance is a new RestartClassLoader but the ClassLoader of the T(some.pkg.FooBar) stays the initial one and therefore the instanceof returns false. I don't know if it's possible, but it would be nice if the class within the Thymeleaf SpEL would be loaded by the same ClassLoader instance that loads the rest of the web application, as this would not alter the application "logic" in that case.

Comment From: philwebb

@mvitz Do you by any chance have a sample application you can provide?

Comment From: mvitz

@philwebb, of course, I put a simple reproducer to https://github.com/mvitz/devtools-thymeleaf-classloader The README contains a screenshot of the index page after the initial start and a second one after adding just a blank line to the Application.java file and letting the DevTools reload happen.

Comment From: philwebb

Thanks for the sample. I'm pretty sure this is caused by org.thymeleaf.spring6.expression.ThymeleafEvaluationContext storing ThymeleafEvaluationContextACLTypeLocator in the TYPE_LOCATOR static. That survives restarts and has a classloader stored inside it.

I'm not really sure how we can fix it, the best thing I can think of so far is to try and reset that static field using reflection.

Comment From: philwebb

I've tried a few reflection hacks, but I don't think we can fix this. I think we need to ask for a change to ThymeleafEvaluationContext. @mvitz Can you please open an issue with https://github.com/thymeleaf/thymeleaf-spring to see if they can update their code.