Skip to content

Commit a122dda

Browse files
committed
Merge branch '6.2.x'
2 parents ebdebbb + 7b08fee commit a122dda

File tree

9 files changed

+61
-25
lines changed

9 files changed

+61
-25
lines changed

Diff for: framework-docs/modules/ROOT/pages/appendix.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ the repeated JNDI lookup overhead. See
9292
{spring-framework-api}++/jndi/JndiLocatorDelegate.html#IGNORE_JNDI_PROPERTY_NAME++[`JndiLocatorDelegate`]
9393
for details.
9494

95+
| `spring.locking.strict`
96+
| Instructs Spring to enforce strict locking during bean creation, rather than the mix of
97+
strict and lenient locking that 6.2 applies by default. See
98+
{spring-framework-api}++/beans/factory/support/DefaultListableBeanFactory.html#STRICT_LOCKING_PROPERTY_NAME++[`DefaultListableBeanFactory`]
99+
for details.
100+
95101
| `spring.objenesis.ignore`
96102
| Instructs Spring to ignore Objenesis, not even attempting to use it. See
97103
{spring-framework-api}++/objenesis/SpringObjenesis.html#IGNORE_OBJENESIS_PROPERTY_NAME++[`SpringObjenesis`]

Diff for: spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.aop.RawTargetAccess;
3737
import org.springframework.aop.TargetSource;
3838
import org.springframework.aop.support.AopUtils;
39+
import org.springframework.aot.AotDetector;
3940
import org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy;
4041
import org.springframework.cglib.core.CodeGenerationException;
4142
import org.springframework.cglib.core.GeneratorStrategy;
@@ -203,7 +204,7 @@ private Object buildProxy(@Nullable ClassLoader classLoader, boolean classOnly)
203204
enhancer.setSuperclass(proxySuperClass);
204205
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
205206
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
206-
enhancer.setAttemptLoad(true);
207+
enhancer.setAttemptLoad(enhancer.getUseCache() && AotDetector.useGeneratedArtifacts());
207208
enhancer.setStrategy(KotlinDetector.isKotlinType(proxySuperClass) ?
208209
new ClassLoaderAwareGeneratorStrategy(classLoader) :
209210
new ClassLoaderAwareGeneratorStrategy(classLoader, undeclaredThrowableStrategy)

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.apache.commons.logging.LogFactory;
2424
import org.jspecify.annotations.Nullable;
2525

26+
import org.springframework.aot.AotDetector;
2627
import org.springframework.beans.BeanInstantiationException;
2728
import org.springframework.beans.BeanUtils;
2829
import org.springframework.beans.factory.BeanFactory;
@@ -153,7 +154,7 @@ public Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
153154
Enhancer enhancer = new Enhancer();
154155
enhancer.setSuperclass(beanDefinition.getBeanClass());
155156
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
156-
enhancer.setAttemptLoad(true);
157+
enhancer.setAttemptLoad(AotDetector.useGeneratedArtifacts());
157158
if (this.owner instanceof ConfigurableBeanFactory cbf) {
158159
ClassLoader cl = cbf.getBeanClassLoader();
159160
enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
130130
implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
131131

132132
/**
133-
* System property that instructs Spring to enforce string locking during bean creation,
133+
* System property that instructs Spring to enforce strict locking during bean creation,
134134
* rather than the mix of strict and lenient locking that 6.2 applies by default. Setting
135135
* this flag to "true" restores 6.1.x style locking in the entire pre-instantiation phase.
136136
* @since 6.2.6

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.jspecify.annotations.Nullable;
2828

2929
import org.springframework.aop.scope.ScopedProxyFactoryBean;
30+
import org.springframework.aot.AotDetector;
3031
import org.springframework.asm.Opcodes;
3132
import org.springframework.asm.Type;
3233
import org.springframework.beans.factory.BeanDefinitionStoreException;
@@ -138,26 +139,22 @@ private Enhancer newEnhancer(Class<?> configSuperClass, @Nullable ClassLoader cl
138139
Enhancer enhancer = new Enhancer();
139140
if (classLoader != null) {
140141
enhancer.setClassLoader(classLoader);
142+
if (classLoader instanceof SmartClassLoader smartClassLoader &&
143+
smartClassLoader.isClassReloadable(configSuperClass)) {
144+
enhancer.setUseCache(false);
145+
}
141146
}
142147
enhancer.setSuperclass(configSuperClass);
143148
enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
144149
enhancer.setUseFactory(false);
145150
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
146-
enhancer.setAttemptLoad(!isClassReloadable(configSuperClass, classLoader));
151+
enhancer.setAttemptLoad(enhancer.getUseCache() && AotDetector.useGeneratedArtifacts());
147152
enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
148153
enhancer.setCallbackFilter(CALLBACK_FILTER);
149154
enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
150155
return enhancer;
151156
}
152157

153-
/**
154-
* Checks whether the given configuration class is reloadable.
155-
*/
156-
private boolean isClassReloadable(Class<?> configSuperClass, @Nullable ClassLoader classLoader) {
157-
return (classLoader instanceof SmartClassLoader smartClassLoader &&
158-
smartClassLoader.isClassReloadable(configSuperClass));
159-
}
160-
161158
/**
162159
* Uses enhancer to generate a subclass of superclass,
163160
* ensuring that callbacks are registered for the new subclass.
@@ -545,7 +542,7 @@ private Object createCglibProxyForFactoryBean(Object factoryBean,
545542
Enhancer enhancer = new Enhancer();
546543
enhancer.setSuperclass(factoryBean.getClass());
547544
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
548-
enhancer.setAttemptLoad(true);
545+
enhancer.setAttemptLoad(AotDetector.useGeneratedArtifacts());
549546
enhancer.setCallbackType(MethodInterceptor.class);
550547

551548
// Ideally create enhanced FactoryBean proxy without constructor side effects,

Diff for: spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassEnhancerTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void withPublicClass() {
7676
classLoader = new BasicSmartClassLoader(getClass().getClassLoader());
7777
enhancedClass = configurationClassEnhancer.enhance(MyConfigWithPublicClass.class, classLoader);
7878
assertThat(MyConfigWithPublicClass.class).isAssignableFrom(enhancedClass);
79-
assertThat(enhancedClass.getClassLoader()).isEqualTo(classLoader.getParent());
79+
assertThat(enhancedClass.getClassLoader()).isEqualTo(classLoader);
8080
}
8181

8282
@Test
@@ -126,7 +126,7 @@ void withNonPublicMethod() {
126126
classLoader = new BasicSmartClassLoader(getClass().getClassLoader());
127127
enhancedClass = configurationClassEnhancer.enhance(MyConfigWithNonPublicMethod.class, classLoader);
128128
assertThat(MyConfigWithNonPublicMethod.class).isAssignableFrom(enhancedClass);
129-
assertThat(enhancedClass.getClassLoader()).isEqualTo(classLoader.getParent());
129+
assertThat(enhancedClass.getClassLoader()).isEqualTo(classLoader);
130130
}
131131

132132

Diff for: spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+36-7
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@
215215
*/
216216
public class PathMatchingResourcePatternResolver implements ResourcePatternResolver {
217217

218+
private static final Resource[] EMPTY_RESOURCE_ARRAY = {};
219+
218220
private static final Log logger = LogFactory.getLog(PathMatchingResourcePatternResolver.class);
219221

220222
/**
@@ -256,6 +258,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
256258

257259
private PathMatcher pathMatcher = new AntPathMatcher();
258260

261+
private boolean useCaches = true;
262+
259263
private final Map<String, Resource[]> rootDirCache = new ConcurrentHashMap<>();
260264

261265
private final Map<String, NavigableSet<String>> jarEntriesCache = new ConcurrentHashMap<>();
@@ -328,6 +332,22 @@ public PathMatcher getPathMatcher() {
328332
return this.pathMatcher;
329333
}
330334

335+
/**
336+
* Specify whether this resolver should use jar caches. Default is {@code true}.
337+
* <p>Switch this flag to {@code false} in order to avoid any jar caching, at
338+
* the {@link JarURLConnection} level as well as within this resolver instance.
339+
* <p>Note that {@link JarURLConnection#setDefaultUseCaches} can be turned off
340+
* independently. This resolver-level setting is designed to only enforce
341+
* {@code JarURLConnection#setUseCaches(false)} if necessary but otherwise
342+
* leaves the JVM-level default in place.
343+
* @since 6.1.19
344+
* @see JarURLConnection#setUseCaches
345+
* @see #clearCache()
346+
*/
347+
public void setUseCaches(boolean useCaches) {
348+
this.useCaches = useCaches;
349+
}
350+
331351

332352
@Override
333353
public Resource getResource(String location) {
@@ -351,7 +371,7 @@ public Resource[] getResources(String locationPattern) throws IOException {
351371
// all class path resources with the given name
352372
Collections.addAll(resources, findAllClassPathResources(locationPatternWithoutPrefix));
353373
}
354-
return resources.toArray(new Resource[0]);
374+
return resources.toArray(EMPTY_RESOURCE_ARRAY);
355375
}
356376
else {
357377
// Generally only look for a pattern after a prefix here,
@@ -395,7 +415,7 @@ protected Resource[] findAllClassPathResources(String location) throws IOExcepti
395415
if (logger.isTraceEnabled()) {
396416
logger.trace("Resolved class path location [" + path + "] to resources " + result);
397417
}
398-
return result.toArray(new Resource[0]);
418+
return result.toArray(EMPTY_RESOURCE_ARRAY);
399419
}
400420

401421
/**
@@ -532,7 +552,9 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
532552
Set<ClassPathManifestEntry> entries = this.manifestEntriesCache;
533553
if (entries == null) {
534554
entries = getClassPathManifestEntries();
535-
this.manifestEntriesCache = entries;
555+
if (this.useCaches) {
556+
this.manifestEntriesCache = entries;
557+
}
536558
}
537559
for (ClassPathManifestEntry entry : entries) {
538560
if (!result.contains(entry.resource()) &&
@@ -684,7 +706,9 @@ else if (commonPrefix.equals(rootDirPath)) {
684706
if (rootDirResources == null) {
685707
// Lookup for specific directory, creating a cache entry for it.
686708
rootDirResources = getResources(rootDirPath);
687-
this.rootDirCache.put(rootDirPath, rootDirResources);
709+
if (this.useCaches) {
710+
this.rootDirCache.put(rootDirPath, rootDirResources);
711+
}
688712
}
689713
}
690714

@@ -716,7 +740,7 @@ else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) {
716740
if (logger.isTraceEnabled()) {
717741
logger.trace("Resolved location pattern [" + locationPattern + "] to resources " + result);
718742
}
719-
return result.toArray(new Resource[0]);
743+
return result.toArray(EMPTY_RESOURCE_ARRAY);
720744
}
721745

722746
/**
@@ -837,6 +861,9 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
837861

838862
if (con instanceof JarURLConnection jarCon) {
839863
// Should usually be the case for traditional JAR files.
864+
if (!this.useCaches) {
865+
jarCon.setUseCaches(false);
866+
}
840867
try {
841868
jarFile = jarCon.getJarFile();
842869
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
@@ -900,8 +927,10 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
900927
}
901928
}
902929
}
903-
// Cache jar entries in TreeSet for efficient searching on re-encounter.
904-
this.jarEntriesCache.put(jarFileUrl, entriesCache);
930+
if (this.useCaches) {
931+
// Cache jar entries in TreeSet for efficient searching on re-encounter.
932+
this.jarEntriesCache.put(jarFileUrl, entriesCache);
933+
}
905934
return result;
906935
}
907936
finally {

Diff for: spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -132,6 +132,7 @@ void encodedHashtagInPath() throws IOException {
132132
Path rootDir = Paths.get("src/test/resources/custom%23root").toAbsolutePath();
133133
URL root = new URL("file:" + rootDir + "/");
134134
resolver = new PathMatchingResourcePatternResolver(new DefaultResourceLoader(new URLClassLoader(new URL[] {root})));
135+
resolver.setUseCaches(false);
135136
assertExactFilenames("classpath*:scanned/*.txt", "resource#test1.txt", "resource#test2.txt");
136137
}
137138

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.commons.logging.LogFactory;
3131
import org.jspecify.annotations.Nullable;
3232

33+
import org.springframework.aot.AotDetector;
3334
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3435
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3536
import org.springframework.cglib.core.SpringNamingPolicy;
@@ -788,7 +789,7 @@ else if (classLoader.getParent() == null) {
788789
enhancer.setSuperclass(controllerType);
789790
enhancer.setInterfaces(new Class<?>[] {MethodInvocationInfo.class});
790791
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
791-
enhancer.setAttemptLoad(true);
792+
enhancer.setAttemptLoad(AotDetector.useGeneratedArtifacts());
792793
enhancer.setCallbackType(MethodInterceptor.class);
793794

794795
Class<?> proxyClass = enhancer.createClass();

0 commit comments

Comments
 (0)