Honor ProtocolResolvers in GenericApplicationContext

When the ProtocolResolver SPI was introduced in Spring Framework 4.3,
support for protocol resolvers was added in DefaultResourceLoader's
getResource() implementation; however, GenericApplicationContext's
overridden getResource() implementation was not updated accordingly.

Prior to this commit, if a GenericApplicationContext was configured
with a custom ResourceLoader, registered protocol resolvers were
ignored.

This commit ensures that protocol resolvers are honored in
GenericApplicationContext even if a custom ResourceLoader is used.

Closes gh-28703
This commit is contained in:
Sam Brannen
2022-06-27 18:00:29 +02:00
parent a970516080
commit 9868c28c73
3 changed files with 78 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
@@ -33,6 +33,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
@@ -86,6 +87,7 @@ import org.springframework.util.Assert;
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 1.1.2
* @see #registerBeanDefinition
* @see #refresh()
@@ -216,13 +218,23 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
//---------------------------------------------------------------------
/**
* This implementation delegates to this context's ResourceLoader if set,
* falling back to the default superclass behavior else.
* @see #setResourceLoader
* This implementation delegates to this context's {@code ResourceLoader} if set,
* falling back to the default superclass behavior otherwise.
* <p>As of Spring Framework 5.3.22, this method also honors registered
* {@linkplain #getProtocolResolvers() protocol resolvers} when a custom
* {@code ResourceLoader} has been set.
* @see #setResourceLoader(ResourceLoader)
* @see #addProtocolResolver(ProtocolResolver)
*/
@Override
public Resource getResource(String location) {
if (this.resourceLoader != null) {
for (ProtocolResolver protocolResolver : getProtocolResolvers()) {
Resource resource = protocolResolver.resolve(location, this);
if (resource != null) {
return resource;
}
}
return this.resourceLoader.getResource(location);
}
return super.getResource(location);
@@ -231,7 +243,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
/**
* This implementation delegates to this context's ResourceLoader if it
* implements the ResourcePatternResolver interface, falling back to the
* default superclass behavior else.
* default superclass behavior otherwise.
* @see #setResourceLoader
*/
@Override