Skip to content

Commit f9ccf4f

Browse files
mp911deodrotbohm
authored andcommitted
DATAMONGO-1017 - Add support for custom implementations in CDI repositories.
Original pull request: #215.
1 parent ab731f4 commit f9ccf4f

File tree

7 files changed

+113
-8
lines changed

7 files changed

+113
-8
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/cdi/MongoRepositoryBean.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
* {@link CdiRepositoryBean} to create Mongo repository instances.
3232
*
3333
* @author Oliver Gierke
34+
* @author Mark Paluch
3435
*/
3536
public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {
3637

@@ -43,11 +44,13 @@ public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {
4344
* @param qualifiers must not be {@literal null}.
4445
* @param repositoryType must not be {@literal null}.
4546
* @param beanManager must not be {@literal null}.
47+
* @param customImplementationBean the bean for the custom implementation of the
48+
* {@link org.springframework.data.repository.Repository}, can be {@literal null}.
4649
*/
4750
public MongoRepositoryBean(Bean<MongoOperations> operations, Set<Annotation> qualifiers, Class<T> repositoryType,
48-
BeanManager beanManager) {
51+
BeanManager beanManager, Bean<?> customImplementationBean) {
4952

50-
super(qualifiers, repositoryType, beanManager);
53+
super(qualifiers, repositoryType, beanManager, customImplementationBean);
5154

5255
Assert.notNull(operations);
5356
this.operations = operations;
@@ -58,11 +61,11 @@ public MongoRepositoryBean(Bean<MongoOperations> operations, Set<Annotation> qua
5861
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class)
5962
*/
6063
@Override
61-
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
64+
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Object customImplementation) {
6265

6366
MongoOperations mongoOperations = getDependencyInstance(operations, MongoOperations.class);
6467
MongoRepositoryFactory factory = new MongoRepositoryFactory(mongoOperations);
6568

66-
return factory.getRepository(repositoryType);
69+
return factory.getRepository(repositoryType, customImplementation);
6770
}
6871
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/cdi/MongoRepositoryExtension.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* CDI extension to export Mongo repositories.
4141
*
4242
* @author Oliver Gierke
43+
* @author Mark Paluch
4344
*/
4445
public class MongoRepositoryExtension extends CdiRepositoryExtensionSupport {
4546

@@ -109,7 +110,10 @@ private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, S
109110
MongoOperations.class.getName(), qualifiers));
110111
}
111112

113+
Bean<?> customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers);
114+
112115
// Construct and return the repository bean.
113-
return new MongoRepositoryBean<T>(mongoOperations, qualifiers, repositoryType, beanManager);
116+
return new MongoRepositoryBean<T>(mongoOperations, qualifiers, repositoryType, beanManager,
117+
customImplementationBean);
114118
}
115119
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/CdiExtensionIntegrationTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
* Integration tests for {@link MongoRepositoryExtension}.
3030
*
3131
* @author Oliver Gierke
32+
* @author Mark Paluch
3233
*/
3334
public class CdiExtensionIntegrationTests {
3435

@@ -61,4 +62,15 @@ public void bootstrapsRepositoryCorrectly() {
6162
assertThat(result, is(notNullValue()));
6263
assertThat(repository.findOne(person.getId()).getId(), is(result.getId()));
6364
}
65+
66+
/**
67+
* @see DATAMONGO-1017
68+
*/
69+
@Test
70+
public void returnOneFromCustomImpl() {
71+
72+
RepositoryClient repositoryConsumer = container.getInstance(RepositoryClient.class);
73+
assertThat(repositoryConsumer.getSamplePersonRepository().returnOne(), is(1));
74+
}
75+
6476
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/RepositoryClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,15 +19,21 @@
1919

2020
/**
2121
* @author Oliver Gierke
22+
* @author Mark Paluch
2223
*/
2324
class RepositoryClient {
2425

2526
@Inject CdiPersonRepository repository;
27+
@Inject SamplePersonRepository samplePersonRepository;
2628

2729
/**
2830
* @return the repository
2931
*/
3032
public CdiPersonRepository getRepository() {
3133
return repository;
3234
}
35+
36+
public SamplePersonRepository getSamplePersonRepository() {
37+
return samplePersonRepository;
38+
}
3339
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2014 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+
* http://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+
package org.springframework.data.mongodb.repository.cdi;
17+
18+
import org.springframework.data.mongodb.core.Person;
19+
import org.springframework.data.repository.Repository;
20+
21+
/**
22+
* @author Mark Paluch
23+
* @see DATAMONGO-1017
24+
*/
25+
public interface SamplePersonRepository extends Repository<Person, Long>, SamplePersonRepositoryCustom {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2014 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+
* http://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.data.mongodb.repository.cdi;
18+
19+
/**
20+
* @see DATAMONGO-1017
21+
* @author Mark Paluch
22+
*/
23+
interface SamplePersonRepositoryCustom {
24+
25+
int returnOne();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2014 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+
* http://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.data.mongodb.repository.cdi;
18+
19+
/**
20+
* @see DATAMONGO-1017
21+
* @author Mark Paluch
22+
*/
23+
class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom {
24+
25+
@Override
26+
public int returnOne() {
27+
return 1;
28+
}
29+
}

0 commit comments

Comments
 (0)