Polish "Rework Spring Session auto-configuration to use customizers"
See gh-32554
This commit is contained in:
parent
ad8753923a
commit
7e47d3bbd6
|
|
@ -45,27 +45,21 @@ import org.springframework.session.hazelcast.config.annotation.web.http.Hazelcas
|
|||
@ConditionalOnMissingBean(SessionRepository.class)
|
||||
@ConditionalOnBean(HazelcastInstance.class)
|
||||
@EnableConfigurationProperties(HazelcastSessionProperties.class)
|
||||
@Import(HazelcastHttpSessionConfiguration.class)
|
||||
class HazelcastSessionConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(HazelcastHttpSessionConfiguration.class)
|
||||
static class SpringBootHazelcastHttpSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, HazelcastSessionProperties hazelcastSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(hazelcastSessionProperties::getMapName).to(sessionRepository::setSessionMapName);
|
||||
map.from(hazelcastSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(hazelcastSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, HazelcastSessionProperties hazelcastSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(hazelcastSessionProperties::getMapName).to(sessionRepository::setSessionMapName);
|
||||
map.from(hazelcastSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(hazelcastSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessi
|
|||
@ConditionalOnMissingBean(SessionRepository.class)
|
||||
@ConditionalOnBean(DataSource.class)
|
||||
@EnableConfigurationProperties(JdbcSessionProperties.class)
|
||||
@Import(DatabaseInitializationDependencyConfigurer.class)
|
||||
@Import({ DatabaseInitializationDependencyConfigurer.class, JdbcHttpSessionConfiguration.class })
|
||||
class JdbcSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
|
|
@ -63,25 +63,19 @@ class JdbcSessionConfiguration {
|
|||
return new JdbcSessionDataSourceScriptDatabaseInitializer(dataSourceToInitialize, properties);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(JdbcHttpSessionConfiguration.class)
|
||||
static class SpringBootJdbcHttpSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, JdbcSessionProperties jdbcSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(jdbcSessionProperties::getTableName).to(sessionRepository::setTableName);
|
||||
map.from(jdbcSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(jdbcSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
map.from(jdbcSessionProperties::getCleanupCron).to(sessionRepository::setCleanupCron);
|
||||
};
|
||||
}
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, JdbcSessionProperties jdbcSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(jdbcSessionProperties::getTableName).to(sessionRepository::setTableName);
|
||||
map.from(jdbcSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(jdbcSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
map.from(jdbcSessionProperties::getCleanupCron).to(sessionRepository::setCleanupCron);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,25 +43,19 @@ import org.springframework.session.data.mongo.config.annotation.web.reactive.Rea
|
|||
@ConditionalOnMissingBean(ReactiveSessionRepository.class)
|
||||
@ConditionalOnBean(ReactiveMongoOperations.class)
|
||||
@EnableConfigurationProperties(MongoSessionProperties.class)
|
||||
@Import(ReactiveMongoWebSessionConfiguration.class)
|
||||
class MongoReactiveSessionConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ReactiveMongoWebSessionConfiguration.class)
|
||||
static class SpringBootMongoWebSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, MongoSessionProperties mongoSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getReactive().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setMaxInactiveIntervalInSeconds((int) timeout.getSeconds()));
|
||||
map.from(mongoSessionProperties::getCollectionName).to(sessionRepository::setCollectionName);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, MongoSessionProperties mongoSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties.determineTimeout(() -> serverProperties.getReactive().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setMaxInactiveIntervalInSeconds((int) timeout.getSeconds()));
|
||||
map.from(mongoSessionProperties::getCollectionName).to(sessionRepository::setCollectionName);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,25 +43,19 @@ import org.springframework.session.data.mongo.config.annotation.web.http.MongoHt
|
|||
@ConditionalOnMissingBean(SessionRepository.class)
|
||||
@ConditionalOnBean(MongoOperations.class)
|
||||
@EnableConfigurationProperties(MongoSessionProperties.class)
|
||||
@Import(MongoHttpSessionConfiguration.class)
|
||||
class MongoSessionConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(MongoHttpSessionConfiguration.class)
|
||||
static class SpringBootMongoHttpSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<MongoIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, MongoSessionProperties mongoSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setMaxInactiveIntervalInSeconds((int) timeout.getSeconds()));
|
||||
map.from(mongoSessionProperties::getCollectionName).to(sessionRepository::setCollectionName);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<MongoIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, MongoSessionProperties mongoSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setMaxInactiveIntervalInSeconds((int) timeout.getSeconds()));
|
||||
map.from(mongoSessionProperties::getCollectionName).to(sessionRepository::setCollectionName);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,26 +43,20 @@ import org.springframework.session.data.redis.config.annotation.web.server.Redis
|
|||
@ConditionalOnMissingBean(ReactiveSessionRepository.class)
|
||||
@ConditionalOnBean(ReactiveRedisConnectionFactory.class)
|
||||
@EnableConfigurationProperties(RedisSessionProperties.class)
|
||||
@Import(RedisWebSessionConfiguration.class)
|
||||
class RedisReactiveSessionConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(RedisWebSessionConfiguration.class)
|
||||
static class SpringBootRedisWebSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getReactive().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
|
||||
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties.determineTimeout(() -> serverProperties.getReactive().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
|
||||
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,40 +57,35 @@ class RedisSessionConfiguration {
|
|||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(prefix = "spring.session.redis", name = "repository-type", havingValue = "default",
|
||||
matchIfMissing = true)
|
||||
@Import(RedisHttpSessionConfiguration.class)
|
||||
static class DefaultRedisSessionConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(RedisHttpSessionConfiguration.class)
|
||||
static class SpringBootRedisHttpSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<RedisSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
String cleanupCron = redisSessionProperties.getCleanupCron();
|
||||
if (cleanupCron != null) {
|
||||
throw new InvalidConfigurationPropertyValueException("spring.session.redis.cleanup-cron",
|
||||
cleanupCron,
|
||||
"Cron-based cleanup is only supported when spring.session.redis.repository-type is set to "
|
||||
+ "indexed.");
|
||||
}
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to(sessionRepository::setDefaultMaxInactiveInterval);
|
||||
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
|
||||
map.from(redisSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
};
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<RedisSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
String cleanupCron = redisSessionProperties.getCleanupCron();
|
||||
if (cleanupCron != null) {
|
||||
throw new InvalidConfigurationPropertyValueException("spring.session.redis.cleanup-cron", cleanupCron,
|
||||
"Cron-based cleanup is only supported when spring.session.redis.repository-type is set to "
|
||||
+ "indexed.");
|
||||
}
|
||||
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to(sessionRepository::setDefaultMaxInactiveInterval);
|
||||
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
|
||||
map.from(redisSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(prefix = "spring.session.redis", name = "repository-type", havingValue = "indexed")
|
||||
@Import(RedisIndexedHttpSessionConfiguration.class)
|
||||
static class IndexedRedisSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
|
|
@ -102,27 +97,20 @@ class RedisSessionConfiguration {
|
|||
};
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(RedisIndexedHttpSessionConfiguration.class)
|
||||
static class SpringBootRedisIndexedHttpSessionConfiguration {
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<RedisIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository
|
||||
.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
|
||||
map.from(redisSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
map.from(redisSessionProperties::getCleanupCron).to(sessionRepository::setCleanupCron);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<RedisIndexedSessionRepository> springBootSessionRepositoryCustomizer(
|
||||
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||
ServerProperties serverProperties) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
return (sessionRepository) -> {
|
||||
map.from(sessionProperties
|
||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
|
||||
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
|
||||
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
|
||||
map.from(redisSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
|
||||
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
|
||||
map.from(redisSessionProperties::getCleanupCron).to(sessionRepository::setCleanupCron);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue