diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java index 3a55067e34..c126e3120c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java @@ -24,9 +24,11 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.session.Session; @@ -42,10 +44,10 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisHt */ @Configuration @ConditionalOnClass(Session.class) -@EnableConfigurationProperties(ServerProperties.class) @AutoConfigureAfter(RedisAutoConfiguration.class) public class SessionAutoConfiguration { + @EnableConfigurationProperties @ConditionalOnClass(RedisConnectionFactory.class) @ConditionalOnWebApplication @ConditionalOnMissingBean(RedisHttpSessionConfiguration.class) @@ -67,6 +69,19 @@ public class SessionAutoConfiguration { } } + @Configuration + @ConditionalOnMissingBean(value = ServerProperties.class, search = SearchStrategy.CURRENT) + // Just in case user switches off ServerPropertiesAutoConfiguration + public static class ServerPropertiesConfiguration { + + @Bean + // Use the same bean name as the default one for any old webapp + public ServerProperties serverProperties() { + return new ServerProperties(); + } + + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java new file mode 100644 index 0000000000..d3a8a2e47f --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -0,0 +1,85 @@ +/* + * Copyright 2012-2015 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.session; + +import org.junit.After; +import org.junit.Test; +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; +import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; +import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.junit.Assert.assertNotNull; + +/** + * @author Dave Syer + * @since 1.3.0 + */ +public class SessionAutoConfigurationTests { + + private AnnotationConfigEmbeddedWebApplicationContext context; + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void flat() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(Config.class, ServerPropertiesAutoConfiguration.class, + RedisAutoConfiguration.class, SessionAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + ServerProperties server = this.context.getBean(ServerProperties.class); + assertNotNull(server); + } + + @Test + public void hierarchy() throws Exception { + AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); + parent.register(RedisAutoConfiguration.class, SessionAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + parent.refresh(); + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.setParent(parent); + this.context.register(Config.class, ServerPropertiesAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + ServerProperties server = this.context.getBean(ServerProperties.class); + assertNotNull(server); + } + + @Configuration + protected static class Config { + + @Bean + public EmbeddedServletContainerFactory containerFactory() { + return new MockEmbeddedServletContainerFactory(); + } + + } + +}