Skip to content

Commit 6238f8b

Browse files
committed
Ensure compatibility with Spring Session module split
This commit updates Spring Session auto-configuration to ensure compatibility with extraction of `SessionRepository` implementations into separate Spring Session modules.
1 parent 885e299 commit 6238f8b

13 files changed

+329
-270
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

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

19+
import org.springframework.beans.factory.ObjectProvider;
1920
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
21+
import org.springframework.boot.autoconfigure.web.ServerProperties;
22+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2023
import org.springframework.context.annotation.Bean;
2124
import org.springframework.context.annotation.Conditional;
2225
import org.springframework.context.annotation.Configuration;
@@ -35,14 +38,23 @@
3538
@EnableSpringHttpSession
3639
@Conditional(SessionCondition.class)
3740
@ConditionalOnMissingBean(SessionRepository.class)
41+
@EnableConfigurationProperties(ServerProperties.class)
3842
class HashMapSessionConfiguration {
3943

44+
private final ServerProperties serverProperties;
45+
46+
HashMapSessionConfiguration(ObjectProvider<ServerProperties> serverProperties) {
47+
this.serverProperties = serverProperties.getIfUnique();
48+
}
49+
4050
@Bean
41-
public MapSessionRepository sessionRepository(SessionProperties properties) {
51+
public MapSessionRepository sessionRepository() {
4252
MapSessionRepository repository = new MapSessionRepository();
43-
Integer timeout = properties.getTimeout();
44-
if (timeout != null) {
45-
repository.setDefaultMaxInactiveInterval(timeout);
53+
if (this.serverProperties != null) {
54+
Integer timeout = this.serverProperties.getSession().getTimeout();
55+
if (timeout != null) {
56+
repository.setDefaultMaxInactiveInterval(timeout);
57+
}
4658
}
4759
return repository;
4860
}

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -16,14 +16,20 @@
1616

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

19+
import javax.annotation.PostConstruct;
20+
1921
import com.hazelcast.core.HazelcastInstance;
2022

21-
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.ObjectProvider;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2326
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.web.ServerProperties;
28+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2429
import org.springframework.context.annotation.Conditional;
2530
import org.springframework.context.annotation.Configuration;
2631
import org.springframework.session.SessionRepository;
32+
import org.springframework.session.hazelcast.HazelcastSessionRepository;
2733
import org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration;
2834

2935
/**
@@ -35,24 +41,39 @@
3541
* @author Vedran Pavic
3642
*/
3743
@Configuration
44+
@ConditionalOnClass(HazelcastSessionRepository.class)
3845
@ConditionalOnMissingBean(SessionRepository.class)
3946
@ConditionalOnBean(HazelcastInstance.class)
4047
@Conditional(SessionCondition.class)
48+
@EnableConfigurationProperties({ ServerProperties.class,
49+
HazelcastSessionProperties.class })
4150
class HazelcastSessionConfiguration {
4251

4352
@Configuration
4453
public static class SpringBootHazelcastHttpSessionConfiguration
4554
extends HazelcastHttpSessionConfiguration {
4655

47-
@Autowired
48-
public void customize(SessionProperties sessionProperties) {
49-
Integer timeout = sessionProperties.getTimeout();
50-
if (timeout != null) {
51-
setMaxInactiveIntervalInSeconds(timeout);
56+
private final HazelcastSessionProperties sessionProperties;
57+
58+
private final ServerProperties serverProperties;
59+
60+
SpringBootHazelcastHttpSessionConfiguration(
61+
HazelcastSessionProperties sessionProperties,
62+
ObjectProvider<ServerProperties> serverProperties) {
63+
this.sessionProperties = sessionProperties;
64+
this.serverProperties = serverProperties.getIfUnique();
65+
}
66+
67+
@PostConstruct
68+
public void init() {
69+
if (this.serverProperties != null) {
70+
Integer timeout = this.serverProperties.getSession().getTimeout();
71+
if (timeout != null) {
72+
setMaxInactiveIntervalInSeconds(timeout);
73+
}
5274
}
53-
SessionProperties.Hazelcast hazelcast = sessionProperties.getHazelcast();
54-
setSessionMapName(hazelcast.getMapName());
55-
setHazelcastFlushMode(hazelcast.getFlushMode());
75+
setSessionMapName(this.sessionProperties.getMapName());
76+
setHazelcastFlushMode(this.sessionProperties.getFlushMode());
5677
}
5778

5879
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.session;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.session.hazelcast.HazelcastFlushMode;
21+
22+
/**
23+
* Configuration properties for Hazelcast backed Spring Session.
24+
*
25+
* @author Vedran Pavic
26+
* @since 2.0.0
27+
*/
28+
@ConfigurationProperties(prefix = "spring.session.hazelcast")
29+
public class HazelcastSessionProperties {
30+
31+
/**
32+
* Name of the map used to store sessions.
33+
*/
34+
private String mapName = "spring:session:sessions";
35+
36+
/**
37+
* Sessions flush mode.
38+
*/
39+
private HazelcastFlushMode flushMode = HazelcastFlushMode.ON_SAVE;
40+
41+
public String getMapName() {
42+
return this.mapName;
43+
}
44+
45+
public void setMapName(String mapName) {
46+
this.mapName = mapName;
47+
}
48+
49+
public HazelcastFlushMode getFlushMode() {
50+
return this.flushMode;
51+
}
52+
53+
public void setFlushMode(HazelcastFlushMode flushMode) {
54+
this.flushMode = flushMode;
55+
}
56+
57+
}

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -16,18 +16,22 @@
1616

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

19+
import javax.annotation.PostConstruct;
1920
import javax.sql.DataSource;
2021

21-
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.beans.factory.ObjectProvider;
2223
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.web.ServerProperties;
27+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2528
import org.springframework.context.annotation.Bean;
2629
import org.springframework.context.annotation.Conditional;
2730
import org.springframework.context.annotation.Configuration;
2831
import org.springframework.core.io.ResourceLoader;
2932
import org.springframework.jdbc.core.JdbcTemplate;
3033
import org.springframework.session.SessionRepository;
34+
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
3135
import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration;
3236

3337
/**
@@ -38,31 +42,44 @@
3842
* @author Vedran Pavic
3943
*/
4044
@Configuration
41-
@ConditionalOnClass(JdbcTemplate.class)
45+
@ConditionalOnClass({ JdbcTemplate.class, JdbcOperationsSessionRepository.class })
4246
@ConditionalOnMissingBean(SessionRepository.class)
4347
@ConditionalOnBean(DataSource.class)
4448
@Conditional(SessionCondition.class)
49+
@EnableConfigurationProperties({ ServerProperties.class, JdbcSessionProperties.class })
4550
class JdbcSessionConfiguration {
4651

4752
@Bean
4853
@ConditionalOnMissingBean
4954
public JdbcSessionDatabaseInitializer jdbcSessionDatabaseInitializer(
5055
DataSource dataSource, ResourceLoader resourceLoader,
51-
SessionProperties properties) {
56+
JdbcSessionProperties properties) {
5257
return new JdbcSessionDatabaseInitializer(dataSource, resourceLoader, properties);
5358
}
5459

5560
@Configuration
5661
public static class SpringBootJdbcHttpSessionConfiguration
5762
extends JdbcHttpSessionConfiguration {
5863

59-
@Autowired
60-
public void customize(SessionProperties sessionProperties) {
61-
Integer timeout = sessionProperties.getTimeout();
62-
if (timeout != null) {
63-
setMaxInactiveIntervalInSeconds(timeout);
64+
private final JdbcSessionProperties sessionProperties;
65+
66+
private final ServerProperties serverProperties;
67+
68+
SpringBootJdbcHttpSessionConfiguration(JdbcSessionProperties sessionProperties,
69+
ObjectProvider<ServerProperties> serverProperties) {
70+
this.sessionProperties = sessionProperties;
71+
this.serverProperties = serverProperties.getIfUnique();
72+
}
73+
74+
@PostConstruct
75+
public void init() {
76+
if (this.serverProperties != null) {
77+
Integer timeout = this.serverProperties.getSession().getTimeout();
78+
if (timeout != null) {
79+
setMaxInactiveIntervalInSeconds(timeout);
80+
}
6481
}
65-
setTableName(sessionProperties.getJdbc().getTableName());
82+
setTableName(this.sessionProperties.getTableName());
6683
}
6784

6885
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
*/
3131
public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer {
3232

33-
private final SessionProperties.Jdbc properties;
33+
private final JdbcSessionProperties properties;
3434

3535
public JdbcSessionDatabaseInitializer(DataSource dataSource,
36-
ResourceLoader resourceLoader, SessionProperties properties) {
36+
ResourceLoader resourceLoader, JdbcSessionProperties properties) {
3737
super(dataSource, resourceLoader);
38-
Assert.notNull(properties, "SessionProperties must not be null");
39-
this.properties = properties.getJdbc();
38+
Assert.notNull(properties, "JdbcSessionProperties must not be null");
39+
this.properties = properties;
4040
}
4141

4242
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.session;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for JDBC backed Spring Session.
23+
*
24+
* @author Vedran Pavic
25+
* @since 2.0.0
26+
*/
27+
@ConfigurationProperties(prefix = "spring.session.jdbc")
28+
public class JdbcSessionProperties {
29+
30+
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
31+
+ "session/jdbc/schema-@@platform@@.sql";
32+
33+
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
34+
35+
/**
36+
* Path to the SQL file to use to initialize the database schema.
37+
*/
38+
private String schema = DEFAULT_SCHEMA_LOCATION;
39+
40+
/**
41+
* Name of database table used to store sessions.
42+
*/
43+
private String tableName = DEFAULT_TABLE_NAME;
44+
45+
private final Initializer initializer = new Initializer();
46+
47+
public String getSchema() {
48+
return this.schema;
49+
}
50+
51+
public void setSchema(String schema) {
52+
this.schema = schema;
53+
}
54+
55+
public String getTableName() {
56+
return this.tableName;
57+
}
58+
59+
public void setTableName(String tableName) {
60+
this.tableName = tableName;
61+
}
62+
63+
public Initializer getInitializer() {
64+
return this.initializer;
65+
}
66+
67+
public class Initializer {
68+
69+
/**
70+
* Create the required session tables on startup if necessary. Enabled
71+
* automatically if the default table name is set or a custom schema is
72+
* configured.
73+
*/
74+
private Boolean enabled;
75+
76+
public boolean isEnabled() {
77+
if (this.enabled != null) {
78+
return this.enabled;
79+
}
80+
boolean defaultTableName = DEFAULT_TABLE_NAME.equals(getTableName());
81+
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(getSchema());
82+
return (defaultTableName || customSchema);
83+
}
84+
85+
public void setEnabled(boolean enabled) {
86+
this.enabled = enabled;
87+
}
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)