Merge pull request #9554 from vpavic:align-session-2.0

* pr/9554:
  Polish "Ensure compatibility with Spring Session module split"
  Ensure compatibility with Spring Session module split
This commit is contained in:
Stephane Nicoll 2017-06-23 10:35:33 +02:00
commit d333bd1a93
9 changed files with 242 additions and 172 deletions

View File

@ -20,10 +20,13 @@ import com.hazelcast.core.HazelcastInstance;
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.SessionRepository;
import org.springframework.session.hazelcast.HazelcastSessionRepository;
import org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration;
/**
@ -35,9 +38,11 @@ import org.springframework.session.hazelcast.config.annotation.web.http.Hazelcas
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(HazelcastSessionRepository.class)
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(HazelcastInstance.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties(HazelcastSessionProperties.class)
class HazelcastSessionConfiguration {
@Configuration
@ -45,14 +50,14 @@ class HazelcastSessionConfiguration {
extends HazelcastHttpSessionConfiguration {
@Autowired
public void customize(SessionProperties sessionProperties) {
public void customize(SessionProperties sessionProperties,
HazelcastSessionProperties hazelcastSessionProperties) {
Integer timeout = sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
SessionProperties.Hazelcast hazelcast = sessionProperties.getHazelcast();
setSessionMapName(hazelcast.getMapName());
setHazelcastFlushMode(hazelcast.getFlushMode());
setSessionMapName(hazelcastSessionProperties.getMapName());
setHazelcastFlushMode(hazelcastSessionProperties.getFlushMode());
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.boot.context.properties.ConfigurationProperties;
import org.springframework.session.hazelcast.HazelcastFlushMode;
/**
* Configuration properties for Hazelcast backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.hazelcast")
public class HazelcastSessionProperties {
/**
* Name of the map used to store sessions.
*/
private String mapName = "spring:session:sessions";
/**
* Sessions flush mode.
*/
private HazelcastFlushMode flushMode = HazelcastFlushMode.ON_SAVE;
public String getMapName() {
return this.mapName;
}
public void setMapName(String mapName) {
this.mapName = mapName;
}
public HazelcastFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(HazelcastFlushMode flushMode) {
this.flushMode = flushMode;
}
}

View File

@ -22,12 +22,14 @@ 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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration;
/**
@ -38,17 +40,18 @@ import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessi
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(JdbcTemplate.class)
@ConditionalOnClass({ JdbcTemplate.class, JdbcOperationsSessionRepository.class })
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(DataSource.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties(JdbcSessionProperties.class)
class JdbcSessionConfiguration {
@Bean
@ConditionalOnMissingBean
public JdbcSessionDatabaseInitializer jdbcSessionDatabaseInitializer(
DataSource dataSource, ResourceLoader resourceLoader,
SessionProperties properties) {
JdbcSessionProperties properties) {
return new JdbcSessionDatabaseInitializer(dataSource, resourceLoader, properties);
}
@ -57,12 +60,13 @@ class JdbcSessionConfiguration {
extends JdbcHttpSessionConfiguration {
@Autowired
public void customize(SessionProperties sessionProperties) {
public void customize(SessionProperties sessionProperties,
JdbcSessionProperties jdbcSessionProperties) {
Integer timeout = sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
setTableName(sessionProperties.getJdbc().getTableName());
setTableName(jdbcSessionProperties.getTableName());
}
}

View File

@ -30,13 +30,13 @@ import org.springframework.util.Assert;
*/
public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer {
private final SessionProperties.Jdbc properties;
private final JdbcSessionProperties properties;
public JdbcSessionDatabaseInitializer(DataSource dataSource,
ResourceLoader resourceLoader, SessionProperties properties) {
ResourceLoader resourceLoader, JdbcSessionProperties properties) {
super(dataSource, resourceLoader);
Assert.notNull(properties, "SessionProperties must not be null");
this.properties = properties.getJdbc();
Assert.notNull(properties, "JdbcSessionProperties must not be null");
this.properties = properties;
}
@Override

View File

@ -0,0 +1,91 @@
/*
* 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.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for JDBC backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.jdbc")
public class JdbcSessionProperties {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
+ "session/jdbc/schema-@@platform@@.sql";
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
/**
* Path to the SQL file to use to initialize the database schema.
*/
private String schema = DEFAULT_SCHEMA_LOCATION;
/**
* Name of database table used to store sessions.
*/
private String tableName = DEFAULT_TABLE_NAME;
private final Initializer initializer = new Initializer();
public String getSchema() {
return this.schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getTableName() {
return this.tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public Initializer getInitializer() {
return this.initializer;
}
public class Initializer {
/**
* Create the required session tables on startup if necessary. Enabled
* automatically if the default table name is set or a custom schema is
* configured.
*/
private Boolean enabled;
public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled;
}
boolean defaultTableName = DEFAULT_TABLE_NAME.equals(getTableName());
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(getSchema());
return (defaultTableName || customSchema);
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}

View File

@ -20,11 +20,13 @@ 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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
/**
@ -37,10 +39,11 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisHt
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(RedisTemplate.class)
@ConditionalOnClass({ RedisTemplate.class, RedisOperationsSessionRepository.class })
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(RedisConnectionFactory.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties(RedisSessionProperties.class)
class RedisSessionConfiguration {
@Configuration
@ -50,15 +53,15 @@ class RedisSessionConfiguration {
private SessionProperties sessionProperties;
@Autowired
public void customize(SessionProperties sessionProperties) {
public void customize(SessionProperties sessionProperties,
RedisSessionProperties redisSessionProperties) {
this.sessionProperties = sessionProperties;
Integer timeout = this.sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
SessionProperties.Redis redis = this.sessionProperties.getRedis();
setRedisNamespace(redis.getNamespace());
setRedisFlushMode(redis.getFlushMode());
setRedisNamespace(redisSessionProperties.getNamespace());
setRedisFlushMode(redisSessionProperties.getFlushMode());
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.boot.context.properties.ConfigurationProperties;
import org.springframework.session.data.redis.RedisFlushMode;
/**
* Configuration properties for Redis backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.redis")
public class RedisSessionProperties {
/**
* Namespace for keys used to store sessions.
*/
private String namespace = "";
/**
* Sessions flush mode.
*/
private RedisFlushMode flushMode = RedisFlushMode.ON_SAVE;
public String getNamespace() {
return this.namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public RedisFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(RedisFlushMode flushMode) {
this.flushMode = flushMode;
}
}

View File

@ -19,8 +19,6 @@ 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;
import org.springframework.session.data.redis.RedisFlushMode;
import org.springframework.session.hazelcast.HazelcastFlushMode;
/**
* Configuration properties for Spring Session.
@ -40,12 +38,6 @@ public class SessionProperties {
private Integer timeout;
private final Hazelcast hazelcast = new Hazelcast();
private final Jdbc jdbc = new Jdbc();
private final Redis redis = new Redis();
public SessionProperties(ObjectProvider<ServerProperties> serverProperties) {
ServerProperties properties = serverProperties.getIfUnique();
this.timeout = (properties != null ? properties.getSession().getTimeout() : null);
@ -68,143 +60,4 @@ public class SessionProperties {
return this.timeout;
}
public Hazelcast getHazelcast() {
return this.hazelcast;
}
public Jdbc getJdbc() {
return this.jdbc;
}
public Redis getRedis() {
return this.redis;
}
public static class Hazelcast {
/**
* Name of the map used to store sessions.
*/
private String mapName = "spring:session:sessions";
/**
* Sessions flush mode.
*/
private HazelcastFlushMode flushMode = HazelcastFlushMode.ON_SAVE;
public String getMapName() {
return this.mapName;
}
public void setMapName(String mapName) {
this.mapName = mapName;
}
public HazelcastFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(HazelcastFlushMode flushMode) {
this.flushMode = flushMode;
}
}
public static class Jdbc {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
+ "session/jdbc/schema-@@platform@@.sql";
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
/**
* Path to the SQL file to use to initialize the database schema.
*/
private String schema = DEFAULT_SCHEMA_LOCATION;
/**
* Name of database table used to store sessions.
*/
private String tableName = DEFAULT_TABLE_NAME;
private final Initializer initializer = new Initializer();
public String getSchema() {
return this.schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getTableName() {
return this.tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public Initializer getInitializer() {
return this.initializer;
}
public class Initializer {
/**
* Create the required session tables on startup if necessary. Enabled
* automatically if the default table name is set or a custom schema is
* configured.
*/
private Boolean enabled;
public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled;
}
boolean defaultTableName = DEFAULT_TABLE_NAME
.equals(Jdbc.this.getTableName());
boolean customSchema = !DEFAULT_SCHEMA_LOCATION
.equals(Jdbc.this.getSchema());
return (defaultTableName || customSchema);
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
public static class Redis {
/**
* Namespace for keys used to store sessions.
*/
private String namespace = "";
/**
* Sessions flush mode.
*/
private RedisFlushMode flushMode = RedisFlushMode.ON_SAVE;
public String getNamespace() {
return this.namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public RedisFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(RedisFlushMode flushMode) {
this.flushMode = flushMode;
}
}
}

View File

@ -54,8 +54,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("SPRING_SESSION");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();
}
@ -70,8 +70,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("SPRING_SESSION");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isFalse();
this.thrown.expect(BadSqlGrammarException.class);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();
@ -88,8 +88,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("FOO_BAR");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from FOO_BAR")).isEmpty();
}
@ -104,8 +104,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("FOO_BAR");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isFalse();
this.thrown.expect(BadSqlGrammarException.class);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();