Skip to content

Loading from spring.factories may fail with a ClassNotFoundException when the TCCL changes between calls #45984

@sephiroth-j

Description

@sephiroth-j

With the update from v3.4.4 to v3.4.5 we encountered two ClassNotFoundException in combination with Spring Webflux and HATEOAS.

I compared what has changed between these two versions and only the following had changed:

  • Spring Boot (of course)
  • Spring Framework 6.2.5 to 6.2.6
  • Reactor BOM 2024.0.4 to 2024.0.5

After using the previous versions of Spring Framework and Reactor, the error still persists and the only change is Spring Boot. The problem also exists with the current version 3.5.0.

There are two problems, the first relates to CollectionJsonAffordanceModelFactory and the second to HtmlInputTypeFactory.

stacktace-HtmlInputTypeFactory.txt
stacktace-CollectionJsonAffordanceModelFactory.txt

I have created a sample application to reproduce the error.

spring-hateoas-issue.zip

  1. mvn package
  2. java -jar .\target\cl-issue-demo-0.0.1-SNAPSHOT.jar
  3. in another console: curl -v http://localhost:8080/hello

No response is received and the stacktrace is logged.

Java version is 21.

openjdk version "21.0.4" 2024-07-16 LTS
OpenJDK Runtime Environment Temurin-21.0.4+7 (build 21.0.4+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.4+7 (build 21.0.4+7-LTS, mixed mode, sharing)

We were also able to create workarounds for both problems by triggering the static code on the main thread with an ApplicationListener (please see ApplicationConfiguration in the sample app).

@Bean
	public ApplicationListener<ApplicationStartedEvent> hateoasWorkaround() {
		// workaround classloader issues
		return event -> {
			// issue #1: org.springframework.hateoas.mediatype.collectionjson.CollectionJsonAffordanceModelFactory
			var link = org.springframework.hateoas.Link.of("test");
			org.springframework.hateoas.mediatype.Affordances.of(link).afford(HttpMethod.GET).build();
			// issue #2: org.springframework.hateoas.mediatype.html.HtmlInputTypeFactory
			var type = ResolvableType.forClassWithGenerics(Mono.class, ResolvableType.forClassWithGenerics(ResponseEntity.class, TestResponse.class));
			org.springframework.hateoas.mediatype.PropertyUtils.getExposedProperties(type);
		};
	}

Activity

Somesh-coding

Somesh-coding commented on Jun 17, 2025

@Somesh-coding
changed the title [-][Regression] ClassNotFoundException starting with 3.4.5 in combination with Webflux and HATEOAS[/-] [+]ClassNotFoundException starting with 3.4.5 in combination with Webflux and HATEOAS[/+] on Jun 17, 2025
added this to the 3.3.13 milestone on Jun 17, 2025
snicoll

snicoll commented on Jun 17, 2025

@snicoll
Member

Thanks for the report and the sample. This seems to be a regression introduced by 4af0ee20d1e0c86cd2f9.

SpringFactories#getSpringFactoriesInstances is now creating the SpringResourceLoader for META-INF/spring.factories. It's cached which means that further attempt to load any factory from that file won't have the propert classloader as before.

wilkinsona

wilkinsona commented on Jun 17, 2025

@wilkinsona
Member

I think there are conflicting requirements here:

Both of these requirements are reasonable. Unfortunately, as far as I can tell, they cannot be supported at the same time while Framework's cache pollution problem remains.

Prior to 4af0ee2, the cache pollution meant that only the second requirement was met. After it, only the first requirement is met.

Given that the first requirement is new in 3.5, I'm leaning towards reverting 4af0ee2 in at least 3.3.x to restore the previous behavior in our final OSS 3.3.x release. For 3.4.x and 3.5.x we can then hopefully get a fix into Framework so that both requirements can be met.

philwebb

philwebb commented on Jun 17, 2025

@philwebb
Member

+1 to reverting

philwebb

philwebb commented on Jun 18, 2025

@philwebb
Member

We discussed this today and we've decided reverting is the least bad option. We'll ultimately need a fix in Framework.

removed
for: team-meetingAn issue we'd like to discuss as a team to make progress
on Jun 18, 2025
changed the title [-]ClassNotFoundException starting with 3.4.5 in combination with Webflux and HATEOAS[/-] [+]Loading from spring.factories may fail with a ClassNotFoundException when the TCCL changes between calls[/+] on Jun 18, 2025
added a commit that references this issue on Jun 18, 2025
bab9071
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @snicoll@philwebb@wilkinsona@spring-projects-issues@sephiroth-j

      Issue actions

        Loading from spring.factories may fail with a ClassNotFoundException when the TCCL changes between calls · Issue #45984 · spring-projects/spring-boot