diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index f711222c0f..cd5ce1ea7d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -83,7 +83,6 @@ import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.MethodMetadata; import org.springframework.core.type.classreading.MetadataReaderFactory; -import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.javapoet.ClassName; import org.springframework.javapoet.CodeBlock; import org.springframework.lang.Nullable; @@ -284,7 +283,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA "AutowiredAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory: " + beanFactory); } this.beanFactory = clbf; - this.metadataReaderFactory = new SimpleMetadataReaderFactory(clbf.getBeanClassLoader()); + this.metadataReaderFactory = MetadataReaderFactory.create(clbf.getBeanClassLoader()); } diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/CachingMetadataReaderFactory.java b/spring-core/src/main/java/org/springframework/core/type/classreading/CachingMetadataReaderFactory.java index 9d9956fcf2..b29a721538 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/CachingMetadataReaderFactory.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/CachingMetadataReaderFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 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. @@ -35,11 +35,13 @@ import org.springframework.lang.Nullable; * @author Costin Leau * @since 2.5 */ -public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { +public class CachingMetadataReaderFactory implements MetadataReaderFactory { /** Default maximum number of entries for a local MetadataReader cache: 256. */ public static final int DEFAULT_CACHE_LIMIT = 256; + private final MetadataReaderFactory delegate; + /** MetadataReader cache: either local or shared at the ResourceLoader level. */ @Nullable private Map metadataReaderCache; @@ -50,7 +52,7 @@ public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { * using a local resource cache. */ public CachingMetadataReaderFactory() { - super(); + this.delegate = MetadataReaderFactory.create((ClassLoader) null); setCacheLimit(DEFAULT_CACHE_LIMIT); } @@ -60,7 +62,7 @@ public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { * @param classLoader the ClassLoader to use */ public CachingMetadataReaderFactory(@Nullable ClassLoader classLoader) { - super(classLoader); + this.delegate = MetadataReaderFactory.create(classLoader); setCacheLimit(DEFAULT_CACHE_LIMIT); } @@ -72,7 +74,7 @@ public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { * @see DefaultResourceLoader#getResourceCache */ public CachingMetadataReaderFactory(@Nullable ResourceLoader resourceLoader) { - super(resourceLoader); + this.delegate = MetadataReaderFactory.create(resourceLoader); if (resourceLoader instanceof DefaultResourceLoader defaultResourceLoader) { this.metadataReaderCache = defaultResourceLoader.getResourceCache(MetadataReader.class); } @@ -81,7 +83,6 @@ public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { } } - /** * Specify the maximum number of entries for the MetadataReader cache. *

Default is 256 for a local cache, whereas a shared cache is @@ -112,6 +113,10 @@ public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { } } + @Override + public MetadataReader getMetadataReader(String className) throws IOException { + return this.delegate.getMetadataReader(className); + } @Override public MetadataReader getMetadataReader(Resource resource) throws IOException { @@ -119,7 +124,7 @@ public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { // No synchronization necessary... MetadataReader metadataReader = this.metadataReaderCache.get(resource); if (metadataReader == null) { - metadataReader = super.getMetadataReader(resource); + metadataReader = this.delegate.getMetadataReader(resource); this.metadataReaderCache.put(resource, metadataReader); } return metadataReader; @@ -128,14 +133,14 @@ public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory { synchronized (this.metadataReaderCache) { MetadataReader metadataReader = this.metadataReaderCache.get(resource); if (metadataReader == null) { - metadataReader = super.getMetadataReader(resource); + metadataReader = this.delegate.getMetadataReader(resource); this.metadataReaderCache.put(resource, metadataReader); } return metadataReader; } } else { - return super.getMetadataReader(resource); + return this.delegate.getMetadataReader(resource); } }