Skip to content

Commit 7349051

Browse files
committed
Merge pull request #33514 from michaelweidmann
* pr/33514: Polish 'Order SessionRepositoryCustomizer before other customizers' Order SessionRepositoryCustomizer before other customizers Closes gh-33514
2 parents 8015f28 + e05b7e4 commit 7349051

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@
3131
import org.springframework.context.annotation.Conditional;
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.annotation.Import;
34+
import org.springframework.core.Ordered;
35+
import org.springframework.core.annotation.Order;
3436
import org.springframework.jdbc.core.JdbcTemplate;
3537
import org.springframework.session.SessionRepository;
3638
import org.springframework.session.config.SessionRepositoryCustomizer;
@@ -64,6 +66,7 @@ JdbcSessionDataSourceScriptDatabaseInitializer jdbcSessionDataSourceScriptDataba
6466
}
6567

6668
@Bean
69+
@Order(Ordered.HIGHEST_PRECEDENCE)
6770
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepositoryCustomizer(
6871
SessionProperties sessionProperties, JdbcSessionProperties jdbcSessionProperties,
6972
ServerProperties serverProperties) {
@@ -76,7 +79,6 @@ SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepos
7679
map.from(jdbcSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
7780
map.from(jdbcSessionProperties::getCleanupCron).to(sessionRepository::setCleanupCron);
7881
};
79-
8082
}
8183

8284
static class OnJdbcSessionDatasourceInitializationCondition extends OnDatabaseInitializationCondition {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@
4949
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
5050
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
5151
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
52+
import org.springframework.session.jdbc.PostgreSqlJdbcIndexedSessionRepositoryCustomizer;
5253
import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
5354

5455
import static org.assertj.core.api.Assertions.assertThat;
@@ -243,6 +244,23 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredJdbcSessi
243244
.hasBean("customInitializer"));
244245
}
245246

247+
@Test
248+
void whenTheUserDefinesTheirOwnJdbcIndexedSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() {
249+
String expectedCreateSessionAttributeQuery = """
250+
INSERT INTO SPRING_SESSION_ATTRIBUTES (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES)
251+
VALUES (?, ?, ?)
252+
ON CONFLICT (SESSION_PRIMARY_ID, ATTRIBUTE_NAME)
253+
DO UPDATE SET ATTRIBUTE_BYTES = EXCLUDED.ATTRIBUTE_BYTES
254+
""";
255+
this.contextRunner.withUserConfiguration(CustomJdbcIndexedSessionRepositoryCustomizerConfiguration.class)
256+
.withConfiguration(AutoConfigurations.of(JdbcSessionConfiguration.class)).run((context) -> {
257+
JdbcIndexedSessionRepository repository = validateSessionRepository(context,
258+
JdbcIndexedSessionRepository.class);
259+
assertThat(repository).hasFieldOrPropertyWithValue("createSessionAttributeQuery",
260+
expectedCreateSessionAttributeQuery);
261+
});
262+
}
263+
246264
@Configuration
247265
static class SessionDataSourceConfiguration {
248266

@@ -289,4 +307,14 @@ DataSourceScriptDatabaseInitializer customInitializer(DataSource dataSource) {
289307

290308
}
291309

310+
@Configuration
311+
static class CustomJdbcIndexedSessionRepositoryCustomizerConfiguration {
312+
313+
@Bean
314+
PostgreSqlJdbcIndexedSessionRepositoryCustomizer postgreSqlJdbcIndexedSessionRepositoryCustomizer() {
315+
return new PostgreSqlJdbcIndexedSessionRepositoryCustomizer();
316+
}
317+
318+
}
319+
292320
}

0 commit comments

Comments
 (0)