28
28
import org .springframework .context .annotation .Bean ;
29
29
import org .springframework .context .annotation .Configuration ;
30
30
import org .springframework .context .annotation .ImportAware ;
31
- import org .springframework .context .support .PropertySourcesPlaceholderConfigurer ;
32
31
import org .springframework .core .annotation .AnnotationAttributes ;
33
32
import org .springframework .core .convert .ConversionService ;
34
33
import org .springframework .core .convert .support .GenericConversionService ;
37
36
import org .springframework .core .type .AnnotationMetadata ;
38
37
import org .springframework .jdbc .support .lob .LobHandler ;
39
38
import org .springframework .scheduling .annotation .EnableScheduling ;
39
+ import org .springframework .scheduling .annotation .SchedulingConfigurer ;
40
+ import org .springframework .scheduling .config .ScheduledTaskRegistrar ;
41
+ import org .springframework .session .MapSession ;
40
42
import org .springframework .session .config .annotation .web .http .SpringHttpSessionConfiguration ;
41
43
import org .springframework .session .jdbc .JdbcOperationsSessionRepository ;
42
44
import org .springframework .transaction .PlatformTransactionManager ;
59
61
@ Configuration
60
62
@ EnableScheduling
61
63
public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
62
- implements BeanClassLoaderAware , ImportAware , EmbeddedValueResolverAware {
64
+ implements BeanClassLoaderAware , EmbeddedValueResolverAware , ImportAware ,
65
+ SchedulingConfigurer {
63
66
64
- private String tableName ;
67
+ static final String DEFAULT_CLEANUP_CRON = "0 * * * * *" ;
65
68
66
- private Integer maxInactiveIntervalInSeconds ;
69
+ private String tableName = JdbcOperationsSessionRepository . DEFAULT_TABLE_NAME ;
67
70
68
- private LobHandler lobHandler ;
71
+ private Integer maxInactiveIntervalInSeconds = MapSession . DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS ;
69
72
70
- @ Autowired (required = false )
71
- @ Qualifier ("conversionService" )
72
- private ConversionService conversionService ;
73
+ private String cleanupCron = DEFAULT_CLEANUP_CRON ;
74
+
75
+ private DataSource dataSource ;
76
+
77
+ private PlatformTransactionManager transactionManager ;
78
+
79
+ private LobHandler lobHandler ;
73
80
74
81
private ConversionService springSessionConversionService ;
75
82
83
+ private ConversionService conversionService ;
84
+
76
85
private ClassLoader classLoader ;
77
86
78
87
private StringValueResolver embeddedValueResolver ;
79
88
80
89
@ Bean
81
- public JdbcOperationsSessionRepository sessionRepository (
82
- @ SpringSessionDataSource ObjectProvider <DataSource > springSessionDataSource ,
83
- ObjectProvider <DataSource > dataSource ,
84
- PlatformTransactionManager transactionManager ) {
85
- DataSource dataSourceToUse = springSessionDataSource .getIfAvailable ();
86
- if (dataSourceToUse == null ) {
87
- dataSourceToUse = dataSource .getObject ();
88
- }
90
+ public JdbcOperationsSessionRepository sessionRepository () {
89
91
JdbcOperationsSessionRepository sessionRepository = new JdbcOperationsSessionRepository (
90
- dataSourceToUse , transactionManager );
91
- String tableName = getTableName ();
92
- if (StringUtils .hasText (tableName )) {
93
- sessionRepository .setTableName (tableName );
92
+ this .dataSource , this .transactionManager );
93
+ if (StringUtils .hasText (this .tableName )) {
94
+ sessionRepository .setTableName (this .tableName );
94
95
}
95
96
sessionRepository
96
97
.setDefaultMaxInactiveInterval (this .maxInactiveIntervalInSeconds );
@@ -104,35 +105,38 @@ else if (this.conversionService != null) {
104
105
sessionRepository .setConversionService (this .conversionService );
105
106
}
106
107
else {
107
- GenericConversionService conversionService = createConversionServiceWithBeanClassLoader ();
108
- sessionRepository .setConversionService (conversionService );
108
+ sessionRepository
109
+ .setConversionService (createConversionServiceWithBeanClassLoader () );
109
110
}
110
111
return sessionRepository ;
111
112
}
112
113
113
- /**
114
- * This must be a separate method because some ClassLoaders load the entire method
115
- * definition even if an if statement guards against it loading. This means that older
116
- * versions of Spring would cause a NoSuchMethodError if this were defined in
117
- * {@link #sessionRepository(ObjectProvider, ObjectProvider, PlatformTransactionManager)}.
118
- *
119
- * @return the default {@link ConversionService}
120
- */
121
- private GenericConversionService createConversionServiceWithBeanClassLoader () {
122
- GenericConversionService conversionService = new GenericConversionService ();
123
- conversionService .addConverter (Object .class , byte [].class ,
124
- new SerializingConverter ());
125
- conversionService .addConverter (byte [].class , Object .class ,
126
- new DeserializingConverter (this .classLoader ));
127
- return conversionService ;
114
+ public void setTableName (String tableName ) {
115
+ this .tableName = tableName ;
128
116
}
129
117
130
- /* (non-Javadoc)
131
- * @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang.ClassLoader)
132
- */
133
- @ Override
134
- public void setBeanClassLoader (ClassLoader classLoader ) {
135
- this .classLoader = classLoader ;
118
+ public void setMaxInactiveIntervalInSeconds (Integer maxInactiveIntervalInSeconds ) {
119
+ this .maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds ;
120
+ }
121
+
122
+ public void setCleanupCron (String cleanupCron ) {
123
+ this .cleanupCron = cleanupCron ;
124
+ }
125
+
126
+ @ Autowired
127
+ public void setDataSource (
128
+ @ SpringSessionDataSource ObjectProvider <DataSource > springSessionDataSource ,
129
+ ObjectProvider <DataSource > dataSource ) {
130
+ DataSource dataSourceToUse = springSessionDataSource .getIfAvailable ();
131
+ if (dataSourceToUse == null ) {
132
+ dataSourceToUse = dataSource .getObject ();
133
+ }
134
+ this .dataSource = dataSourceToUse ;
135
+ }
136
+
137
+ @ Autowired
138
+ public void setTransactionManager (PlatformTransactionManager transactionManager ) {
139
+ this .transactionManager = transactionManager ;
136
140
}
137
141
138
142
@ Autowired (required = false )
@@ -147,48 +151,53 @@ public void setSpringSessionConversionService(ConversionService conversionServic
147
151
this .springSessionConversionService = conversionService ;
148
152
}
149
153
150
- public void setTableName (String tableName ) {
151
- this .tableName = tableName ;
154
+ @ Autowired (required = false )
155
+ @ Qualifier ("conversionService" )
156
+ public void setConversionService (ConversionService conversionService ) {
157
+ this .conversionService = conversionService ;
152
158
}
153
159
154
- public void setMaxInactiveIntervalInSeconds (Integer maxInactiveIntervalInSeconds ) {
155
- this .maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds ;
160
+ @ Override
161
+ public void setBeanClassLoader (ClassLoader classLoader ) {
162
+ this .classLoader = classLoader ;
156
163
}
157
164
158
- private String getTableName () {
159
- String systemProperty = System .getProperty ("spring.session.jdbc.tableName" , "" );
160
- if (StringUtils .hasText (systemProperty )) {
161
- return systemProperty ;
162
- }
163
- return this .tableName ;
165
+ @ Override
166
+ public void setEmbeddedValueResolver (StringValueResolver resolver ) {
167
+ this .embeddedValueResolver = resolver ;
164
168
}
165
169
166
170
@ Override
167
171
public void setImportMetadata (AnnotationMetadata importMetadata ) {
168
- Map <String , Object > enableAttrMap = importMetadata
172
+ Map <String , Object > attributeMap = importMetadata
169
173
.getAnnotationAttributes (EnableJdbcHttpSession .class .getName ());
170
- AnnotationAttributes enableAttrs = AnnotationAttributes .fromMap (enableAttrMap );
171
- String tableNameValue = enableAttrs .getString ("tableName" );
174
+ AnnotationAttributes attributes = AnnotationAttributes .fromMap (attributeMap );
175
+ String tableNameValue = attributes .getString ("tableName" );
172
176
if (StringUtils .hasText (tableNameValue )) {
173
177
this .tableName = this .embeddedValueResolver
174
178
.resolveStringValue (tableNameValue );
175
179
}
176
- this .maxInactiveIntervalInSeconds = enableAttrs
180
+ this .maxInactiveIntervalInSeconds = attributes
177
181
.getNumber ("maxInactiveIntervalInSeconds" );
182
+ String cleanupCron = attributes .getString ("cleanupCron" );
183
+ if (StringUtils .hasText (cleanupCron )) {
184
+ this .cleanupCron = this .embeddedValueResolver .resolveStringValue (cleanupCron );
185
+ }
178
186
}
179
187
180
188
@ Override
181
- public void setEmbeddedValueResolver (StringValueResolver resolver ) {
182
- this .embeddedValueResolver = resolver ;
189
+ public void configureTasks (ScheduledTaskRegistrar taskRegistrar ) {
190
+ taskRegistrar .addCronTask (() -> sessionRepository ().cleanUpExpiredSessions (),
191
+ this .cleanupCron );
183
192
}
184
193
185
- /**
186
- * Property placeholder to process the @Scheduled annotation.
187
- * @return the {@link PropertySourcesPlaceholderConfigurer} to use
188
- */
189
- @ Bean
190
- public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer () {
191
- return new PropertySourcesPlaceholderConfigurer () ;
194
+ private GenericConversionService createConversionServiceWithBeanClassLoader () {
195
+ GenericConversionService conversionService = new GenericConversionService ();
196
+ conversionService . addConverter ( Object . class , byte []. class ,
197
+ new SerializingConverter ());
198
+ conversionService . addConverter ( byte []. class , Object . class ,
199
+ new DeserializingConverter ( this . classLoader ));
200
+ return conversionService ;
192
201
}
193
202
194
203
}
0 commit comments