diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java index 2cd70ce584..93550ec2db 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.context.ResourceLoaderAware; @@ -37,7 +38,7 @@ import org.springframework.core.io.ResourceLoader; * @since 1.1.0 */ @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false) -public class ResourceProperties implements ResourceLoaderAware { +public class ResourceProperties implements ResourceLoaderAware, InitializingBean { private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" }; @@ -81,6 +82,11 @@ public class ResourceProperties implements ResourceLoaderAware { this.resourceLoader = resourceLoader; } + @Override + public void afterPropertiesSet() { + this.staticLocations = appendSlashIfNecessary(this.staticLocations); + } + public String[] getStaticLocations() { return this.staticLocations; } @@ -93,7 +99,9 @@ public class ResourceProperties implements ResourceLoaderAware { String[] normalized = new String[staticLocations.length]; for (int i = 0; i < staticLocations.length; i++) { String location = staticLocations[i]; - normalized[i] = (location.endsWith("/") ? location : location + "/"); + if (location != null) { + normalized[i] = (location.endsWith("/") ? location : location + "/"); + } } return normalized; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesBindingTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesBindingTests.java new file mode 100644 index 0000000000..3b3a5005d7 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesBindingTests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2012-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.web; + +import org.junit.After; +import org.junit.Test; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Binding tests for {@link ResourceProperties}. + * + * @author Stephane Nicoll + */ +public class ResourcePropertiesBindingTests { + + private AnnotationConfigApplicationContext context; + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void staticLocationsExpandArray() { + ResourceProperties properties = load( + "spring.resources.static-locations[0]=classpath:/one/", + "spring.resources.static-locations[1]=classpath:/two", + "spring.resources.static-locations[2]=classpath:/three/", + "spring.resources.static-locations[3]=classpath:/four", + "spring.resources.static-locations[4]=classpath:/five/", + "spring.resources.static-locations[5]=classpath:/six"); + assertThat(properties.getStaticLocations()).contains("classpath:/one/", + "classpath:/two/", "classpath:/three/", "classpath:/four/", + "classpath:/five/", "classpath:/six/"); + } + + private ResourceProperties load(String... environment) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(ctx, environment); + ctx.register(TestConfiguration.class); + ctx.refresh(); + this.context = ctx; + return this.context.getBean(ResourceProperties.class); + } + + @Configuration + @EnableConfigurationProperties(ResourceProperties.class) + static class TestConfiguration { + + } + +}