Skip to content

Commit f5805e8

Browse files
authored
Merge pull request #1 from paychex/MultiDatasource
Multi datasource
2 parents c507bb2 + 04cc782 commit f5805e8

File tree

3 files changed

+204
-9
lines changed

3 files changed

+204
-9
lines changed

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

+15-2
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;
@@ -52,11 +53,23 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
5253
private String collectionName;
5354
private StringValueResolver embeddedValueResolver;
5455
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+
}
5568

5669
@Bean
57-
public MongoOperationsSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
70+
public MongoOperationsSessionRepository mongoSessionRepository() {
5871

59-
MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations);
72+
MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(this.mongoOperations);
6073
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
6174

6275
if (this.mongoSessionConverter != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014-2019 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+
* https://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.session.data.mongo.config.annotation.web.http;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.beans.factory.annotation.Qualifier;
26+
27+
28+
/**
29+
* Qualifier annotation for a {@link org.springframework.data.mongodb.core.MongoOperations} to be injected in
30+
* {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository}.
31+
* <p>
32+
* This will enable us to have multiple MongoOperations in the application.
33+
*
34+
* @author Visweshwar Ganesh
35+
* @since 2.2.0
36+
*/
37+
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
@Qualifier
41+
public @interface SpringSessionMongoOperations {
42+
43+
}

src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java

+146-7
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
import static org.mockito.ArgumentMatchers.anyString;
2020
import static org.mockito.BDDMockito.*;
2121

22-
import java.net.UnknownHostException;
2322

2423
import org.junit.After;
2524
import org.junit.Rule;
2625
import org.junit.Test;
2726
import org.junit.rules.ExpectedException;
28-
import org.springframework.beans.factory.UnsatisfiedDependencyException;
27+
import org.springframework.beans.factory.BeanCreationException;
2928
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3029
import org.springframework.context.annotation.Bean;
3130
import org.springframework.context.annotation.Configuration;
3231
import org.springframework.context.annotation.Import;
32+
import org.springframework.context.annotation.Primary;
3333
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
3434
import org.springframework.data.mongodb.core.MongoOperations;
3535
import org.springframework.data.mongodb.core.index.IndexOperations;
@@ -65,10 +65,9 @@ public void after() {
6565
@Test
6666
public void noMongoOperationsConfiguration() {
6767

68-
this.thrown.expect(UnsatisfiedDependencyException.class);
69-
this.thrown.expectMessage("mongoSessionRepository");
70-
71-
registerAndRefresh(EmptyConfiguration.class);
68+
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
69+
() -> registerAndRefresh(NoMongoOperationsConfiguration.class)).withMessageContaining(
70+
"expected at least 1 bean which qualifies as autowire candidate");
7271
}
7372

7473
@Test
@@ -155,16 +154,82 @@ private void registerAndRefresh(Class<?>... annotatedClasses) {
155154
this.context.refresh();
156155
}
157156

157+
@Test
158+
public void multipleDataSourceConfiguration() {
159+
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(
160+
() -> registerAndRefresh(MongoOperationConfiguration.class,
161+
MultipleMongoOperationsConfiguration.class)).withMessageContaining(
162+
"expected single matching bean but found 2");
163+
}
164+
165+
@Test
166+
public void primaryMongoOperationConfiguration() {
167+
168+
registerAndRefresh(MongoOperationConfiguration.class, PrimaryMongoOperationsConfiguration.class);
169+
170+
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
171+
MongoOperations mongoOperations = this.context.getBean("primaryMongoOperations", MongoOperations.class);
172+
assertThat(repository).isNotNull();
173+
assertThat(mongoOperations).isNotNull();
174+
MongoOperations mongoOperationsReflection = (MongoOperations) ReflectionTestUtils.getField(repository,
175+
"mongoOperations");
176+
assertThat(mongoOperationsReflection).isNotNull();
177+
assertThat((mongoOperationsReflection)).isEqualTo(mongoOperations);
178+
}
179+
180+
@Test
181+
public void qualifiedDataSourceConfiguration() {
182+
registerAndRefresh(MongoOperationConfiguration.class, QualifiedMongoOperationsConfiguration.class);
183+
184+
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
185+
MongoOperations mongoOperations = this.context.getBean("qualifiedMongoOperations", MongoOperations.class);
186+
assertThat(repository).isNotNull();
187+
assertThat(mongoOperations).isNotNull();
188+
MongoOperations mongoOperationsReflection = (MongoOperations) ReflectionTestUtils.getField(repository,
189+
"mongoOperations");
190+
assertThat(mongoOperationsReflection).isNotNull();
191+
assertThat(mongoOperationsReflection).isEqualTo(mongoOperations);
192+
}
193+
194+
@Test
195+
public void qualifiedAndPrimaryDataSourceConfiguration() {
196+
registerAndRefresh(MongoOperationConfiguration.class, QualifiedAndPrimaryMongoConfiguration.class);
197+
198+
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
199+
MongoOperations mongoOperations = this.context.getBean("qualifiedMongoOperations", MongoOperations.class);
200+
assertThat(repository).isNotNull();
201+
assertThat(mongoOperations).isNotNull();
202+
MongoOperations mongoOperationsReflection = (MongoOperations) ReflectionTestUtils.getField(repository,
203+
"mongoOperations");
204+
assertThat(mongoOperations).isNotNull();
205+
assertThat(mongoOperationsReflection).isEqualTo(mongoOperations);
206+
}
207+
158208
@Configuration
159209
@EnableMongoHttpSession
160210
static class EmptyConfiguration {
161211

162212
}
163213

214+
@Configuration
215+
static class MongoOperationConfiguration {
216+
217+
@Bean
218+
public MongoOperations defaultMongoOperations() {
219+
MongoOperations mongoOperations = mock(MongoOperations.class);
220+
IndexOperations indexOperations = mock(IndexOperations.class);
221+
222+
given(mongoOperations.indexOps(anyString())).willReturn(indexOperations);
223+
224+
return mongoOperations;
225+
}
226+
227+
}
228+
164229
static class BaseConfiguration {
165230

166231
@Bean
167-
public MongoOperations mongoOperations() throws UnknownHostException {
232+
public MongoOperations mongoOperations() {
168233

169234
MongoOperations mongoOperations = mock(MongoOperations.class);
170235
IndexOperations indexOperations = mock(IndexOperations.class);
@@ -241,4 +306,78 @@ public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer
241306

242307
}
243308

309+
@EnableMongoHttpSession
310+
static class NoMongoOperationsConfiguration {
311+
312+
}
313+
314+
@EnableMongoHttpSession
315+
static class MultipleMongoOperationsConfiguration {
316+
317+
@Bean
318+
public MongoOperations secondaryDataSource() {
319+
return mock(MongoOperations.class);
320+
}
321+
322+
}
323+
324+
@EnableMongoHttpSession
325+
static class PrimaryMongoOperationsConfiguration {
326+
327+
@Bean
328+
@Primary
329+
public MongoOperations primaryMongoOperations() {
330+
MongoOperations mongoOperations = mock(MongoOperations.class);
331+
IndexOperations indexOperations = mock(IndexOperations.class);
332+
333+
given(mongoOperations.indexOps(anyString())).willReturn(indexOperations);
334+
335+
return mongoOperations;
336+
}
337+
338+
}
339+
340+
@EnableMongoHttpSession
341+
static class QualifiedMongoOperationsConfiguration {
342+
343+
@Bean
344+
@SpringSessionMongoOperations
345+
public MongoOperations qualifiedMongoOperations() {
346+
MongoOperations mongoOperations = mock(MongoOperations.class);
347+
IndexOperations indexOperations = mock(IndexOperations.class);
348+
349+
given(mongoOperations.indexOps(anyString())).willReturn(indexOperations);
350+
351+
return mongoOperations;
352+
}
353+
354+
}
355+
356+
@EnableMongoHttpSession
357+
static class QualifiedAndPrimaryMongoConfiguration {
358+
359+
@Bean
360+
@SpringSessionMongoOperations
361+
public MongoOperations qualifiedMongoOperations() {
362+
MongoOperations mongoOperations = mock(MongoOperations.class);
363+
IndexOperations indexOperations = mock(IndexOperations.class);
364+
365+
given(mongoOperations.indexOps(anyString())).willReturn(indexOperations);
366+
367+
return mongoOperations;
368+
}
369+
370+
@Bean
371+
@Primary
372+
public MongoOperations primaryMongoOperations() {
373+
MongoOperations mongoOperations = mock(MongoOperations.class);
374+
IndexOperations indexOperations = mock(IndexOperations.class);
375+
376+
given(mongoOperations.indexOps(anyString())).willReturn(indexOperations);
377+
378+
return mongoOperations;
379+
}
380+
381+
}
382+
244383
}

0 commit comments

Comments
 (0)