|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2022 the original author or authors. |
| 2 | + * Copyright 2014-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 |
|
25 | 25 | import javax.sql.DataSource;
|
26 | 26 |
|
| 27 | +import org.springframework.beans.BeansException; |
27 | 28 | import org.springframework.beans.factory.BeanClassLoaderAware;
|
| 29 | +import org.springframework.beans.factory.InitializingBean; |
28 | 30 | import org.springframework.beans.factory.ObjectProvider;
|
29 | 31 | import org.springframework.beans.factory.annotation.Autowired;
|
30 | 32 | import org.springframework.beans.factory.annotation.Qualifier;
|
| 33 | +import org.springframework.context.ApplicationContext; |
| 34 | +import org.springframework.context.ApplicationContextAware; |
31 | 35 | import org.springframework.context.EmbeddedValueResolverAware;
|
32 | 36 | import org.springframework.context.annotation.Bean;
|
33 | 37 | import org.springframework.context.annotation.Configuration;
|
|
56 | 60 | import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
|
57 | 61 | import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
|
58 | 62 | import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
|
| 63 | +import org.springframework.session.jdbc.config.annotation.SpringSessionTransactionManager; |
59 | 64 | import org.springframework.session.web.http.SessionRepositoryFilter;
|
60 | 65 | import org.springframework.transaction.PlatformTransactionManager;
|
61 | 66 | import org.springframework.transaction.TransactionDefinition;
|
|
79 | 84 | */
|
80 | 85 | @Configuration(proxyBeanMethods = false)
|
81 | 86 | @Import(SpringHttpSessionConfiguration.class)
|
82 |
| -public class JdbcHttpSessionConfiguration implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware { |
| 87 | +public class JdbcHttpSessionConfiguration implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware, |
| 88 | + ApplicationContextAware, InitializingBean { |
83 | 89 |
|
84 | 90 | private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
|
85 | 91 |
|
@@ -113,6 +119,21 @@ public class JdbcHttpSessionConfiguration implements BeanClassLoaderAware, Embed
|
113 | 119 |
|
114 | 120 | private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
|
115 | 121 |
|
| 122 | + private ApplicationContext applicationContext; |
| 123 | + |
| 124 | + @Override |
| 125 | + public void afterPropertiesSet() throws Exception { |
| 126 | + if (this.transactionOperations == null && this.transactionManager == null) { |
| 127 | + this.transactionManager = getUniqueTransactionManager(); |
| 128 | + if (this.transactionManager == null) { |
| 129 | + throw new IllegalStateException( |
| 130 | + """ |
| 131 | + Could not resolve an unique PlatformTransactionManager bean from the application context. |
| 132 | + Please provide either a TransactionOperations bean named springSessionTransactionOperations or a PlatformTransactionManager bean qualified with @SpringSessionTransactionManager"""); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
116 | 137 | @Bean
|
117 | 138 | public JdbcIndexedSessionRepository sessionRepository() {
|
118 | 139 | JdbcTemplate jdbcTemplate = createJdbcTemplate(this.dataSource);
|
@@ -200,7 +221,8 @@ public void setDataSource(@SpringSessionDataSource ObjectProvider<DataSource> sp
|
200 | 221 | this.dataSource = dataSourceToUse;
|
201 | 222 | }
|
202 | 223 |
|
203 |
| - @Autowired |
| 224 | + @Autowired(required = false) |
| 225 | + @SpringSessionTransactionManager |
204 | 226 | public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
205 | 227 | this.transactionManager = transactionManager;
|
206 | 228 | }
|
@@ -276,14 +298,23 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
|
276 | 298 | this.saveMode = attributes.getEnum("saveMode");
|
277 | 299 | }
|
278 | 300 |
|
| 301 | + @Override |
| 302 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 303 | + this.applicationContext = applicationContext; |
| 304 | + } |
| 305 | + |
| 306 | + private PlatformTransactionManager getUniqueTransactionManager() { |
| 307 | + return this.applicationContext.getBeanProvider(PlatformTransactionManager.class).getIfUnique(); |
| 308 | + } |
| 309 | + |
279 | 310 | private static JdbcTemplate createJdbcTemplate(DataSource dataSource) {
|
280 | 311 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
281 | 312 | jdbcTemplate.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(dataSource));
|
282 | 313 | jdbcTemplate.afterPropertiesSet();
|
283 | 314 | return jdbcTemplate;
|
284 | 315 | }
|
285 | 316 |
|
286 |
| - private static TransactionTemplate createTransactionTemplate(PlatformTransactionManager transactionManager) { |
| 317 | + private TransactionTemplate createTransactionTemplate(PlatformTransactionManager transactionManager) { |
287 | 318 | TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
|
288 | 319 | transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
289 | 320 | transactionTemplate.afterPropertiesSet();
|
|
0 commit comments