diff --git a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java index 0bc2a6612352..19e1321378de 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -18,10 +18,12 @@ import java.beans.PropertyEditorSupport; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; import org.apache.commons.logging.Log; @@ -33,6 +35,7 @@ import org.springframework.core.io.Resource; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Editor for {@link org.springframework.core.io.Resource} arrays, to @@ -50,6 +53,7 @@ * * @author Juergen Hoeller * @author Chris Beams + * @author Yanming Zhou * @since 1.1.2 * @see org.springframework.core.io.Resource * @see ResourcePatternResolver @@ -108,17 +112,33 @@ public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolv /** - * Treat the given text as a location pattern and convert it to a Resource array. + * Treat the given text as a location pattern or comma delimited location patterns and convert it to a Resource array. */ @Override public void setAsText(String text) { String pattern = resolvePath(text).trim(); - try { - setValue(this.resourcePatternResolver.getResources(pattern)); + if (pattern.indexOf(',') > 0) { + List resources = new ArrayList<>(); + for (String locationPattern : StringUtils.commaDelimitedListToStringArray(pattern)) { + try { + for (Resource resource : this.resourcePatternResolver.getResources(locationPattern)) { + resources.add(resource); + } + } + catch (IOException ex) { + throw new IllegalArgumentException( + "Could not resolve resource location pattern [" + locationPattern + "]: " + ex.getMessage()); + } + } + setValue(resources.toArray()); } - catch (IOException ex) { - throw new IllegalArgumentException( - "Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage()); + else { + try { + setValue(this.resourcePatternResolver.getResources(pattern)); + } catch (IOException ex) { + throw new IllegalArgumentException( + "Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage()); + } } } diff --git a/spring-core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java b/spring-core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java index 4bb19cdc33de..2f2a02263861 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2013 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. @@ -21,6 +21,8 @@ import org.junit.jupiter.api.Test; import org.springframework.core.env.StandardEnvironment; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileUrlResource; import org.springframework.core.io.Resource; import static org.assertj.core.api.Assertions.assertThat; @@ -29,6 +31,7 @@ /** * @author Dave Syer * @author Juergen Hoeller + * @author Yanming Zhou */ class ResourceArrayPropertyEditorTests { @@ -83,4 +86,13 @@ void strictSystemPropertyReplacementWithUnresolvablePlaceholder() { } } + @Test + void commaDelimitedResources() { + PropertyEditor editor = new ResourceArrayPropertyEditor(); + editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class,file:/foo/bar.txt"); + Resource[] resources = (Resource[]) editor.getValue(); + assertThat(resources).isNotNull(); + assertThat(resources[0]).isInstanceOf(ClassPathResource.class); + assertThat(resources[1]).isInstanceOf(FileUrlResource.class); + } }