Throw exception if classpath*: is used

Update `ConfigFileApplicationListener` to throw a better exception if
`classpath*:` is used as a location.

Closes gh-21168
pull/22089/head
Phillip Webb 5 years ago
parent 16005c608f
commit e2705b2cfd

@ -62,6 +62,7 @@ import org.springframework.core.env.PropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@ -618,6 +619,8 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
for (String path : asResolvedSet(this.environment.getProperty(propertyName), null)) {
if (!path.contains("$")) {
path = StringUtils.cleanPath(path);
Assert.state(!path.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX),
"Classpath wildard patterns cannot be used as a search location");
if (!ResourceUtils.isUrl(path)) {
path = ResourceUtils.FILE_URL_PREFIX + path;
}

@ -63,6 +63,7 @@ import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link ConfigFileApplicationListener}.
@ -648,6 +649,15 @@ public class ConfigFileApplicationListenerTests {
matchingPropertySource("applicationConfig: [file:" + location.replace(File.separatorChar, '/') + "]"));
}
@Test
public void classpathWildcardResourceThrowsException() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location=classpath*:override.properties");
assertThatIllegalStateException()
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
.withMessage("Classpath wildard patterns cannot be used as a search location");
}
@Test
public void propertySourceAnnotation() {
SpringApplication application = new SpringApplication(WithPropertySource.class);

Loading…
Cancel
Save