From 27f8a63f1c394f438be7465ac50b35b9ad33cd04 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 23 Jun 2017 10:34:17 +0200 Subject: [PATCH] Polish "Ensure compatibility with Spring Session module split" Closes gh-9554 --- .../session/HashMapSessionConfiguration.java | 20 ++---- .../HazelcastSessionConfiguration.java | 38 ++++------- .../session/JdbcSessionConfiguration.java | 33 +++------- .../session/RedisSessionConfiguration.java | 33 ++++------ .../session/SessionAutoConfiguration.java | 14 ++--- .../session/SessionProperties.java | 63 +++++++++++++++++++ ...itional-spring-configuration-metadata.json | 5 -- .../SessionAutoConfigurationTests.java | 7 +++ 8 files changed, 112 insertions(+), 101 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java index 00c4428aaa..28f161ea0f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java @@ -16,10 +16,7 @@ package org.springframework.boot.autoconfigure.session; -import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -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.Conditional; import org.springframework.context.annotation.Configuration; @@ -38,23 +35,14 @@ import org.springframework.session.config.annotation.web.http.EnableSpringHttpSe @EnableSpringHttpSession @Conditional(SessionCondition.class) @ConditionalOnMissingBean(SessionRepository.class) -@EnableConfigurationProperties(ServerProperties.class) class HashMapSessionConfiguration { - private final ServerProperties serverProperties; - - HashMapSessionConfiguration(ObjectProvider serverProperties) { - this.serverProperties = serverProperties.getIfUnique(); - } - @Bean - public MapSessionRepository sessionRepository() { + public MapSessionRepository sessionRepository(SessionProperties properties) { MapSessionRepository repository = new MapSessionRepository(); - if (this.serverProperties != null) { - Integer timeout = this.serverProperties.getSession().getTimeout(); - if (timeout != null) { - repository.setDefaultMaxInactiveInterval(timeout); - } + Integer timeout = properties.getTimeout(); + if (timeout != null) { + repository.setDefaultMaxInactiveInterval(timeout); } return repository; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java index a620bf8d2f..b959c12dc7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2016 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. @@ -16,15 +16,12 @@ package org.springframework.boot.autoconfigure.session; -import javax.annotation.PostConstruct; - import com.hazelcast.core.HazelcastInstance; -import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @@ -45,35 +42,22 @@ import org.springframework.session.hazelcast.config.annotation.web.http.Hazelcas @ConditionalOnMissingBean(SessionRepository.class) @ConditionalOnBean(HazelcastInstance.class) @Conditional(SessionCondition.class) -@EnableConfigurationProperties({ ServerProperties.class, - HazelcastSessionProperties.class }) +@EnableConfigurationProperties(HazelcastSessionProperties.class) class HazelcastSessionConfiguration { @Configuration public static class SpringBootHazelcastHttpSessionConfiguration extends HazelcastHttpSessionConfiguration { - private final HazelcastSessionProperties sessionProperties; - - private final ServerProperties serverProperties; - - SpringBootHazelcastHttpSessionConfiguration( - HazelcastSessionProperties sessionProperties, - ObjectProvider serverProperties) { - this.sessionProperties = sessionProperties; - this.serverProperties = serverProperties.getIfUnique(); - } - - @PostConstruct - public void init() { - if (this.serverProperties != null) { - Integer timeout = this.serverProperties.getSession().getTimeout(); - if (timeout != null) { - setMaxInactiveIntervalInSeconds(timeout); - } + @Autowired + public void customize(SessionProperties sessionProperties, + HazelcastSessionProperties hazelcastSessionProperties) { + Integer timeout = sessionProperties.getTimeout(); + if (timeout != null) { + setMaxInactiveIntervalInSeconds(timeout); } - setSessionMapName(this.sessionProperties.getMapName()); - setHazelcastFlushMode(this.sessionProperties.getFlushMode()); + setSessionMapName(hazelcastSessionProperties.getMapName()); + setHazelcastFlushMode(hazelcastSessionProperties.getFlushMode()); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java index 1a9b72fbe1..7f2019a7f4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2016 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. @@ -16,14 +16,12 @@ package org.springframework.boot.autoconfigure.session; -import javax.annotation.PostConstruct; import javax.sql.DataSource; -import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -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.Conditional; @@ -46,7 +44,7 @@ import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessi @ConditionalOnMissingBean(SessionRepository.class) @ConditionalOnBean(DataSource.class) @Conditional(SessionCondition.class) -@EnableConfigurationProperties({ ServerProperties.class, JdbcSessionProperties.class }) +@EnableConfigurationProperties(JdbcSessionProperties.class) class JdbcSessionConfiguration { @Bean @@ -61,25 +59,14 @@ class JdbcSessionConfiguration { public static class SpringBootJdbcHttpSessionConfiguration extends JdbcHttpSessionConfiguration { - private final JdbcSessionProperties sessionProperties; - - private final ServerProperties serverProperties; - - SpringBootJdbcHttpSessionConfiguration(JdbcSessionProperties sessionProperties, - ObjectProvider serverProperties) { - this.sessionProperties = sessionProperties; - this.serverProperties = serverProperties.getIfUnique(); - } - - @PostConstruct - public void init() { - if (this.serverProperties != null) { - Integer timeout = this.serverProperties.getSession().getTimeout(); - if (timeout != null) { - setMaxInactiveIntervalInSeconds(timeout); - } + @Autowired + public void customize(SessionProperties sessionProperties, + JdbcSessionProperties jdbcSessionProperties) { + Integer timeout = sessionProperties.getTimeout(); + if (timeout != null) { + setMaxInactiveIntervalInSeconds(timeout); } - setTableName(this.sessionProperties.getTableName()); + setTableName(jdbcSessionProperties.getTableName()); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java index fdb6230f08..cb8a0db77a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java @@ -16,13 +16,10 @@ package org.springframework.boot.autoconfigure.session; -import javax.annotation.PostConstruct; - -import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @@ -46,33 +43,25 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisHt @ConditionalOnMissingBean(SessionRepository.class) @ConditionalOnBean(RedisConnectionFactory.class) @Conditional(SessionCondition.class) -@EnableConfigurationProperties({ ServerProperties.class, RedisSessionProperties.class }) +@EnableConfigurationProperties(RedisSessionProperties.class) class RedisSessionConfiguration { @Configuration public static class SpringBootRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration { - private final RedisSessionProperties sessionProperties; - - private final ServerProperties serverProperties; + private SessionProperties sessionProperties; - SpringBootRedisHttpSessionConfiguration(RedisSessionProperties sessionProperties, - ObjectProvider serverProperties) { + @Autowired + public void customize(SessionProperties sessionProperties, + RedisSessionProperties redisSessionProperties) { this.sessionProperties = sessionProperties; - this.serverProperties = serverProperties.getIfUnique(); - } - - @PostConstruct - public void init() { - if (this.serverProperties != null) { - Integer timeout = this.serverProperties.getSession().getTimeout(); - if (timeout != null) { - setMaxInactiveIntervalInSeconds(timeout); - } + Integer timeout = this.sessionProperties.getTimeout(); + if (timeout != null) { + setMaxInactiveIntervalInSeconds(timeout); } - setRedisNamespace(this.sessionProperties.getNamespace()); - setRedisFlushMode(this.sessionProperties.getFlushMode()); + setRedisNamespace(redisSessionProperties.getNamespace()); + setRedisFlushMode(redisSessionProperties.getFlushMode()); } } 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 fc1fdc18bf..f1c9fb32fc 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 @@ -31,11 +31,10 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.SessionConfigurationImportSelector; import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.SessionRepositoryValidator; -import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; -import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotationMetadata; import org.springframework.session.Session; import org.springframework.session.SessionRepository; @@ -54,6 +53,7 @@ import org.springframework.session.SessionRepository; @ConditionalOnMissingBean(SessionRepository.class) @ConditionalOnClass(Session.class) @ConditionalOnWebApplication(type = Type.SERVLET) +@EnableConfigurationProperties(SessionProperties.class) @AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, RedisAutoConfiguration.class }) @Import({ SessionConfigurationImportSelector.class, SessionRepositoryValidator.class, @@ -83,21 +83,19 @@ public class SessionAutoConfiguration { */ static class SessionRepositoryValidator { - private Environment environment; + private SessionProperties sessionProperties; private ObjectProvider> sessionRepositoryProvider; - SessionRepositoryValidator(Environment environment, + SessionRepositoryValidator(SessionProperties sessionProperties, ObjectProvider> sessionRepositoryProvider) { - this.environment = environment; + this.sessionProperties = sessionProperties; this.sessionRepositoryProvider = sessionRepositoryProvider; } @PostConstruct public void checkSessionRepository() { - Binder binder = Binder.get(this.environment); - StoreType storeType = binder - .bind("spring.session.store-type", StoreType.class).orElse(null); + StoreType storeType = this.sessionProperties.getStoreType(); if (storeType != StoreType.NONE && this.sessionRepositoryProvider.getIfAvailable() == null) { if (storeType != null) { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java new file mode 100644 index 0000000000..f647ecfb65 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java @@ -0,0 +1,63 @@ +/* + * Copyright 2012-2017 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.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for Spring Session. + * + * @author Tommy Ludwig + * @author Stephane Nicoll + * @author Vedran Pavic + * @since 1.4.0 + */ +@ConfigurationProperties(prefix = "spring.session") +public class SessionProperties { + + /** + * Session store type. + */ + private StoreType storeType; + + private Integer timeout; + + public SessionProperties(ObjectProvider serverProperties) { + ServerProperties properties = serverProperties.getIfUnique(); + this.timeout = (properties != null ? properties.getSession().getTimeout() : null); + } + + public StoreType getStoreType() { + return this.storeType; + } + + public void setStoreType(StoreType storeType) { + this.storeType = storeType; + } + + /** + * Return the session timeout in seconds. + * @return the session timeout in seconds + * @see ServerProperties#getSession() + */ + public Integer getTimeout() { + return this.timeout; + } + +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index c60bf0cc5d..04bf53776c 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -383,11 +383,6 @@ "name": "spring.session.redis.flush-mode", "defaultValue": "on-save" }, - { - "name": "spring.session.store-type", - "type": "org.springframework.boot.autoconfigure.session.StoreType", - "description": "Session store type." - }, { "name": "spring.social.auto-connection-views", "type": "java.lang.Boolean", 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 index 3c9917a63d..90eabbccac 100644 --- 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 @@ -96,6 +96,13 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat assertThat(getSessionTimeout(repository)).isEqualTo(3000); } + @Test + public void springSessionTimeoutIsNotAValidProperty() { + this.thrown.expect(BeanCreationException.class); + this.thrown.expectMessage("Could not bind"); + load("spring.session.store-type=hash-map", "spring.session.timeout=3000"); + } + @SuppressWarnings("unchecked") @Test public void filterIsRegisteredWithAsyncErrorAndRequestDispatcherTypes() {