|
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;
|
|
54 | 58 | import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
|
55 | 59 | import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
|
56 | 60 | import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
|
| 61 | +import org.springframework.session.jdbc.config.annotation.SpringSessionTransactionManager; |
57 | 62 | import org.springframework.session.web.http.SessionRepositoryFilter;
|
58 | 63 | import org.springframework.transaction.PlatformTransactionManager;
|
59 | 64 | import org.springframework.transaction.TransactionDefinition;
|
|
77 | 82 | */
|
78 | 83 | @Configuration(proxyBeanMethods = false)
|
79 | 84 | @Import(SpringHttpSessionConfiguration.class)
|
80 |
| -public class JdbcHttpSessionConfiguration implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware { |
| 85 | +public class JdbcHttpSessionConfiguration implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware, |
| 86 | + ApplicationContextAware, InitializingBean { |
81 | 87 |
|
82 | 88 | private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
|
83 | 89 |
|
@@ -109,6 +115,21 @@ public class JdbcHttpSessionConfiguration implements BeanClassLoaderAware, Embed
|
109 | 115 |
|
110 | 116 | private StringValueResolver embeddedValueResolver;
|
111 | 117 |
|
| 118 | + private ApplicationContext applicationContext; |
| 119 | + |
| 120 | + @Override |
| 121 | + public void afterPropertiesSet() throws Exception { |
| 122 | + if (this.transactionOperations == null && this.transactionManager == null) { |
| 123 | + this.transactionManager = getUniqueTransactionManager(); |
| 124 | + if (this.transactionManager == null) { |
| 125 | + throw new IllegalStateException( |
| 126 | + """ |
| 127 | + Could not resolve an unique PlatformTransactionManager bean from the application context. |
| 128 | + Please provide either a TransactionOperations bean named springSessionTransactionOperations or a PlatformTransactionManager bean qualified with @SpringSessionTransactionManager"""); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
112 | 133 | @Bean
|
113 | 134 | public JdbcIndexedSessionRepository sessionRepository() {
|
114 | 135 | JdbcTemplate jdbcTemplate = createJdbcTemplate(this.dataSource);
|
@@ -195,7 +216,8 @@ public void setDataSource(@SpringSessionDataSource ObjectProvider<DataSource> sp
|
195 | 216 | this.dataSource = dataSourceToUse;
|
196 | 217 | }
|
197 | 218 |
|
198 |
| - @Autowired |
| 219 | + @Autowired(required = false) |
| 220 | + @SpringSessionTransactionManager |
199 | 221 | public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
200 | 222 | this.transactionManager = transactionManager;
|
201 | 223 | }
|
@@ -266,14 +288,23 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
|
266 | 288 | this.saveMode = attributes.getEnum("saveMode");
|
267 | 289 | }
|
268 | 290 |
|
| 291 | + @Override |
| 292 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 293 | + this.applicationContext = applicationContext; |
| 294 | + } |
| 295 | + |
| 296 | + private PlatformTransactionManager getUniqueTransactionManager() { |
| 297 | + return this.applicationContext.getBeanProvider(PlatformTransactionManager.class).getIfUnique(); |
| 298 | + } |
| 299 | + |
269 | 300 | private static JdbcTemplate createJdbcTemplate(DataSource dataSource) {
|
270 | 301 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
271 | 302 | jdbcTemplate.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(dataSource));
|
272 | 303 | jdbcTemplate.afterPropertiesSet();
|
273 | 304 | return jdbcTemplate;
|
274 | 305 | }
|
275 | 306 |
|
276 |
| - private static TransactionTemplate createTransactionTemplate(PlatformTransactionManager transactionManager) { |
| 307 | + private TransactionTemplate createTransactionTemplate(PlatformTransactionManager transactionManager) { |
277 | 308 | TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
|
278 | 309 | transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
279 | 310 | transactionTemplate.afterPropertiesSet();
|
|
0 commit comments