From 4af0ee20d1e0c86cd2f94ff3ecf7d5d3887d8cb3 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 9 Apr 2025 14:49:17 -0700 Subject: [PATCH] Work around Spring Framework cache pollution bug Update `SpringApplication` to work around `SpringFactoriesLoader` cache pollution by loading factories using a `null` class loader. See https://github.com/spring-projects/spring-framework/pull/34732 for details. --- .../org/springframework/boot/SpringApplication.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 480d9a94d3e..4ccef6605c8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -480,7 +480,7 @@ public class SpringApplication { } private List getSpringFactoriesInstances(Class type, ArgumentResolver argumentResolver) { - return SpringFactoriesLoader.forDefaultResourceLocation(getClassLoader()).load(type, argumentResolver); + return SpringFactoriesLoader.forDefaultResourceLocation(getClassLoader(null)).load(type, argumentResolver); } private ConfigurableEnvironment getOrCreateEnvironment() { @@ -715,10 +715,11 @@ public class SpringApplication { * @return a ClassLoader (never null) */ public ClassLoader getClassLoader() { - if (this.resourceLoader != null) { - return this.resourceLoader.getClassLoader(); - } - return ClassUtils.getDefaultClassLoader(); + return getClassLoader(ClassUtils.getDefaultClassLoader()); + } + + private ClassLoader getClassLoader(ClassLoader fallback) { + return (this.resourceLoader != null) ? this.resourceLoader.getClassLoader() : fallback; } /**