Polish "Ensure compatibility with Spring Session module split"

Closes gh-9554
pull/9566/merge
Stephane Nicoll 8 years ago
parent ccb1eaf8ed
commit 27f8a63f1c

@ -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> 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;
}

@ -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> 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());
}
}

@ -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> 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());
}
}

@ -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> 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());
}
}

@ -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<SessionRepository<?>> sessionRepositoryProvider;
SessionRepositoryValidator(Environment environment,
SessionRepositoryValidator(SessionProperties sessionProperties,
ObjectProvider<SessionRepository<?>> 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) {

@ -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) {
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;
}
}

@ -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",

@ -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() {

Loading…
Cancel
Save