Skip to content

Commit 88eaa08

Browse files
committed
How to configure additional Datasource for Spring Session spring-projects#96
1 parent c507bb2 commit 88eaa08

File tree

3 files changed

+422
-209
lines changed

3 files changed

+422
-209
lines changed

src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.java

+82-69
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.time.Duration;
1919

2020
import org.springframework.beans.factory.BeanClassLoaderAware;
21+
import org.springframework.beans.factory.ObjectProvider;
2122
import org.springframework.beans.factory.annotation.Autowired;
2223
import org.springframework.context.EmbeddedValueResolverAware;
2324
import org.springframework.context.annotation.Bean;
@@ -45,74 +46,86 @@
4546
*/
4647
@Configuration
4748
public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguration
48-
implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware {
49-
50-
private AbstractMongoSessionConverter mongoSessionConverter;
51-
private Integer maxInactiveIntervalInSeconds;
52-
private String collectionName;
53-
private StringValueResolver embeddedValueResolver;
54-
private ClassLoader classLoader;
55-
56-
@Bean
57-
public MongoOperationsSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
58-
59-
MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations);
60-
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
61-
62-
if (this.mongoSessionConverter != null) {
63-
repository.setMongoSessionConverter(this.mongoSessionConverter);
64-
} else {
65-
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
66-
new DeserializingConverter(this.classLoader),
67-
Duration.ofSeconds(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL));
68-
repository.setMongoSessionConverter(mongoSessionConverter);
69-
}
70-
71-
if (StringUtils.hasText(this.collectionName)) {
72-
repository.setCollectionName(this.collectionName);
73-
}
74-
75-
return repository;
76-
}
77-
78-
public void setCollectionName(String collectionName) {
79-
this.collectionName = collectionName;
80-
}
81-
82-
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
83-
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
84-
}
85-
86-
public void setImportMetadata(AnnotationMetadata importMetadata) {
87-
88-
AnnotationAttributes attributes = AnnotationAttributes
89-
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName()));
90-
91-
if (attributes != null) {
92-
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
93-
} else {
94-
this.maxInactiveIntervalInSeconds = MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL;
95-
}
96-
97-
String collectionNameValue = attributes != null ? attributes.getString("collectionName") : "";
98-
if (StringUtils.hasText(collectionNameValue)) {
99-
this.collectionName = this.embeddedValueResolver.resolveStringValue(collectionNameValue);
100-
}
101-
}
102-
103-
@Autowired(required = false)
104-
public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) {
105-
this.mongoSessionConverter = mongoSessionConverter;
106-
}
107-
108-
@Override
109-
public void setBeanClassLoader(ClassLoader classLoader) {
110-
this.classLoader = classLoader;
111-
}
112-
113-
@Override
114-
public void setEmbeddedValueResolver(StringValueResolver resolver) {
115-
this.embeddedValueResolver = resolver;
116-
}
49+
implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware {
50+
51+
private AbstractMongoSessionConverter mongoSessionConverter;
52+
private Integer maxInactiveIntervalInSeconds;
53+
private String collectionName;
54+
private StringValueResolver embeddedValueResolver;
55+
private ClassLoader classLoader;
56+
private MongoOperations mongoOperations;
57+
58+
@Autowired
59+
public void setMongoOperations(
60+
@SpringSessionMongoOperations ObjectProvider<MongoOperations> springSessionMongoOperations,
61+
ObjectProvider<MongoOperations> mongoOperations) {
62+
MongoOperations mongoOperationsToUse = springSessionMongoOperations.getIfAvailable();
63+
if (mongoOperationsToUse == null) {
64+
mongoOperationsToUse = mongoOperations.getObject();
65+
}
66+
this.mongoOperations = mongoOperationsToUse;
67+
}
68+
69+
@Bean
70+
public MongoOperationsSessionRepository mongoSessionRepository() {
71+
72+
MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations);
73+
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
74+
75+
if (this.mongoSessionConverter != null) {
76+
repository.setMongoSessionConverter(this.mongoSessionConverter);
77+
} else {
78+
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
79+
new DeserializingConverter(this.classLoader),
80+
Duration.ofSeconds(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL));
81+
repository.setMongoSessionConverter(mongoSessionConverter);
82+
}
83+
84+
if (StringUtils.hasText(this.collectionName)) {
85+
repository.setCollectionName(this.collectionName);
86+
}
87+
88+
return repository;
89+
}
90+
91+
public void setCollectionName(String collectionName) {
92+
this.collectionName = collectionName;
93+
}
94+
95+
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
96+
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
97+
}
98+
99+
public void setImportMetadata(AnnotationMetadata importMetadata) {
100+
101+
AnnotationAttributes attributes = AnnotationAttributes
102+
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName()));
103+
104+
if (attributes != null) {
105+
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
106+
} else {
107+
this.maxInactiveIntervalInSeconds = MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL;
108+
}
109+
110+
String collectionNameValue = attributes != null ? attributes.getString("collectionName") : "";
111+
if (StringUtils.hasText(collectionNameValue)) {
112+
this.collectionName = this.embeddedValueResolver.resolveStringValue(collectionNameValue);
113+
}
114+
}
115+
116+
@Autowired(required = false)
117+
public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) {
118+
this.mongoSessionConverter = mongoSessionConverter;
119+
}
120+
121+
@Override
122+
public void setBeanClassLoader(ClassLoader classLoader) {
123+
this.classLoader = classLoader;
124+
}
125+
126+
@Override
127+
public void setEmbeddedValueResolver(StringValueResolver resolver) {
128+
this.embeddedValueResolver = resolver;
129+
}
117130

118131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.springframework.session.data.mongo.config.annotation.web.http;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
5+
import java.lang.annotation.*;
6+
7+
/*
8+
* Copyright 2019 the original author or authors.
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* https://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
/**
23+
* Qualifier annotation for a {@link org.springframework.data.mongodb.core.MongoOperations} to be injected in
24+
* {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository}.
25+
*
26+
* This will enable us to have multiple MongoOperations in the application.
27+
*
28+
* @author Visweshwar Ganesh
29+
* @since 2.x.x
30+
*/
31+
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE,
32+
ElementType.ANNOTATION_TYPE })
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Documented
35+
@Qualifier
36+
public @interface SpringSessionMongoOperations {
37+
38+
}

0 commit comments

Comments
 (0)