Skip to content

Commit e0a7bd8

Browse files
vpavicwilkinsona
authored andcommitted
Fix deprecation warnings in Spring Session auto-configuration
This commit updates Spring Session auto-configuration to avoid usage of deprecated methods, and moves to newly introduced Duration based defaultMaxInactiveInterval setters across all session repository implementations. Additionally, this fixes several tests that are broken due to session repository implementations now using Duration type for their defaultMaxInactiveInterval fields. See gh-32633
1 parent 006d2ed commit e0a7bd8

12 files changed

+26
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> springBootSession
5555
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
5656
return (sessionRepository) -> {
5757
map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
58-
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
58+
.to(sessionRepository::setDefaultMaxInactiveInterval);
5959
map.from(hazelcastSessionProperties::getMapName).to(sessionRepository::setSessionMapName);
6060
map.from(hazelcastSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
6161
map.from(hazelcastSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepos
7070
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
7171
return (sessionRepository) -> {
7272
map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
73-
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
73+
.to(sessionRepository::setDefaultMaxInactiveInterval);
7474
map.from(jdbcSessionProperties::getTableName).to(sessionRepository::setTableName);
7575
map.from(jdbcSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
7676
map.from(jdbcSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> springBootSe
5353
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
5454
return (sessionRepository) -> {
5555
map.from(sessionProperties.determineTimeout(() -> serverProperties.getReactive().getSession().getTimeout()))
56-
.to((timeout) -> sessionRepository.setMaxInactiveIntervalInSeconds((int) timeout.getSeconds()));
56+
.to(sessionRepository::setDefaultMaxInactiveInterval);
5757
map.from(mongoSessionProperties::getCollectionName).to(sessionRepository::setCollectionName);
5858
};
5959
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ SessionRepositoryCustomizer<MongoIndexedSessionRepository> springBootSessionRepo
5353
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
5454
return (sessionRepository) -> {
5555
map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
56-
.to((timeout) -> sessionRepository.setMaxInactiveIntervalInSeconds((int) timeout.getSeconds()));
56+
.to(sessionRepository::setDefaultMaxInactiveInterval);
5757
map.from(mongoSessionProperties::getCollectionName).to(sessionRepository::setCollectionName);
5858
};
5959
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> springBootSe
5353
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
5454
return (sessionRepository) -> {
5555
map.from(sessionProperties.determineTimeout(() -> serverProperties.getReactive().getSession().getTimeout()))
56-
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
56+
.to(sessionRepository::setDefaultMaxInactiveInterval);
5757
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
5858
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
5959
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ SessionRepositoryCustomizer<RedisIndexedSessionRepository> springBootSessionRepo
105105
return (sessionRepository) -> {
106106
map.from(sessionProperties
107107
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
108-
.to((timeout) -> sessionRepository.setDefaultMaxInactiveInterval((int) timeout.getSeconds()));
108+
.to(sessionRepository::setDefaultMaxInactiveInterval);
109109
map.from(redisSessionProperties::getNamespace).to(sessionRepository::setRedisKeyNamespace);
110110
map.from(redisSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
111111
map.from(redisSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
3737
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
3838
import org.springframework.http.ResponseCookie;
39+
import org.springframework.session.MapSession;
3940
import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
4041
import org.springframework.session.data.redis.ReactiveRedisSessionRepository;
4142

@@ -72,7 +73,8 @@ void defaultConfigWithCustomTimeout() {
7273
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl()).run((context) -> {
7374
ReactiveMongoSessionRepository repository = validateSessionRepository(context,
7475
ReactiveMongoSessionRepository.class);
75-
assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 60);
76+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval",
77+
Duration.ofMinutes(1));
7678
});
7779
}
7880

@@ -82,7 +84,8 @@ void defaultConfigWithCustomSessionTimeout() {
8284
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl()).run((context) -> {
8385
ReactiveMongoSessionRepository repository = validateSessionRepository(context,
8486
ReactiveMongoSessionRepository.class);
85-
assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 60);
87+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval",
88+
Duration.ofMinutes(1));
8689
});
8790
}
8891

@@ -126,8 +129,8 @@ private ContextConsumer<AssertableReactiveWebApplicationContext> validateSpringS
126129
ReactiveMongoSessionRepository repository = validateSessionRepository(context,
127130
ReactiveMongoSessionRepository.class);
128131
assertThat(repository.getCollectionName()).isEqualTo(collectionName);
129-
assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds",
130-
ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL);
132+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval",
133+
MapSession.DEFAULT_MAX_INACTIVE_INTERVAL);
131134
};
132135
}
133136

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void defaultConfigWithCustomTimeout() {
7878
this.contextRunner.withPropertyValues("spring.session.timeout=1m").run((context) -> {
7979
ReactiveRedisSessionRepository repository = validateSessionRepository(context,
8080
ReactiveRedisSessionRepository.class);
81-
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", 60);
81+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", Duration.ofMinutes(1));
8282
});
8383
}
8484

@@ -87,7 +87,7 @@ void defaultConfigWithCustomWebFluxTimeout() {
8787
this.contextRunner.withPropertyValues("server.reactive.session.timeout=1m").run((context) -> {
8888
ReactiveRedisSessionRepository repository = validateSessionRepository(context,
8989
ReactiveRedisSessionRepository.class);
90-
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", 60);
90+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", Duration.ofMinutes(1));
9191
});
9292
}
9393

@@ -124,7 +124,7 @@ private ContextConsumer<AssertableReactiveWebApplicationContext> validateSpringS
124124
ReactiveRedisSessionRepository repository = validateSessionRepository(context,
125125
ReactiveRedisSessionRepository.class);
126126
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval",
127-
MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
127+
MapSession.DEFAULT_MAX_INACTIVE_INTERVAL);
128128
assertThat(repository).hasFieldOrPropertyWithValue("namespace", namespace);
129129
assertThat(repository).hasFieldOrPropertyWithValue("saveMode", saveMode);
130130
};

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.session;
1818

19+
import java.time.Duration;
20+
1921
import com.hazelcast.core.HazelcastInstance;
2022
import com.hazelcast.map.IMap;
2123
import org.junit.jupiter.api.Test;
@@ -69,15 +71,15 @@ void defaultConfigWithCustomTimeout() {
6971
this.contextRunner.withPropertyValues("spring.session.timeout=1m").run((context) -> {
7072
HazelcastIndexedSessionRepository repository = validateSessionRepository(context,
7173
HazelcastIndexedSessionRepository.class);
72-
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", 60);
74+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", Duration.ofMinutes(1));
7375
});
7476
}
7577

7678
private void validateDefaultConfig(AssertableWebApplicationContext context) {
7779
HazelcastIndexedSessionRepository repository = validateSessionRepository(context,
7880
HazelcastIndexedSessionRepository.class);
7981
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval",
80-
(int) new ServerProperties().getServlet().getSession().getTimeout().getSeconds());
82+
new ServerProperties().getServlet().getSession().getTimeout());
8183
HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class);
8284
then(hazelcastInstance).should().getMap("spring:session:sessions");
8385
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.session;
1818

19+
import java.time.Duration;
20+
1921
import javax.sql.DataSource;
2022

2123
import org.apache.commons.dbcp2.BasicDataSource;
@@ -83,7 +85,7 @@ private void validateDefaultConfig(AssertableWebApplicationContext context) {
8385
JdbcIndexedSessionRepository repository = validateSessionRepository(context,
8486
JdbcIndexedSessionRepository.class);
8587
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval",
86-
(int) new ServerProperties().getServlet().getSession().getTimeout().getSeconds());
88+
new ServerProperties().getServlet().getSession().getTimeout());
8789
assertThat(repository).hasFieldOrPropertyWithValue("tableName", "SPRING_SESSION");
8890
assertThat(repository).hasFieldOrPropertyWithValue("cleanupCron", "0 * * * * *");
8991
assertThat(context.getBean(JdbcSessionProperties.class).getInitializeSchema())
@@ -118,7 +120,7 @@ void customTimeout() {
118120
this.contextRunner.withPropertyValues("spring.session.timeout=1m").run((context) -> {
119121
JdbcIndexedSessionRepository repository = validateSessionRepository(context,
120122
JdbcIndexedSessionRepository.class);
121-
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", 60);
123+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", Duration.ofMinutes(1));
122124
});
123125
}
124126

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ private ContextConsumer<AssertableWebApplicationContext> validateSpringSessionUs
8686
MongoIndexedSessionRepository repository = validateSessionRepository(context,
8787
MongoIndexedSessionRepository.class);
8888
assertThat(repository).hasFieldOrPropertyWithValue("collectionName", collectionName);
89-
assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds",
90-
(int) timeout.getSeconds());
89+
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", timeout);
9190
};
9291
}
9392

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private ContextConsumer<AssertableWebApplicationContext> validateSpringSessionUs
186186
RedisIndexedSessionRepository repository = validateSessionRepository(context,
187187
RedisIndexedSessionRepository.class);
188188
assertThat(repository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval",
189-
(int) new ServerProperties().getServlet().getSession().getTimeout().getSeconds());
189+
new ServerProperties().getServlet().getSession().getTimeout());
190190
assertThat(repository).hasFieldOrPropertyWithValue("namespace", keyNamespace);
191191
assertThat(repository).hasFieldOrPropertyWithValue("flushMode", flushMode);
192192
assertThat(repository).hasFieldOrPropertyWithValue("saveMode", saveMode);

0 commit comments

Comments
 (0)