DATAMONGO-1017 - Add support for custom implementations in CDI repositories.

Original pull request: #215.
This commit is contained in:
Mark Paluch
2014-08-07 20:37:40 +02:00
committed by Oliver Gierke
parent ab731f40a7
commit f9ccf4f532
7 changed files with 113 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ import org.springframework.util.Assert;
* {@link CdiRepositoryBean} to create Mongo repository instances.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {
@@ -43,11 +44,13 @@ public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {
* @param qualifiers must not be {@literal null}.
* @param repositoryType must not be {@literal null}.
* @param beanManager must not be {@literal null}.
* @param customImplementationBean the bean for the custom implementation of the
* {@link org.springframework.data.repository.Repository}, can be {@literal null}.
*/
public MongoRepositoryBean(Bean<MongoOperations> operations, Set<Annotation> qualifiers, Class<T> repositoryType,
BeanManager beanManager) {
BeanManager beanManager, Bean<?> customImplementationBean) {
super(qualifiers, repositoryType, beanManager);
super(qualifiers, repositoryType, beanManager, customImplementationBean);
Assert.notNull(operations);
this.operations = operations;
@@ -58,11 +61,11 @@ public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class)
*/
@Override
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Object customImplementation) {
MongoOperations mongoOperations = getDependencyInstance(operations, MongoOperations.class);
MongoRepositoryFactory factory = new MongoRepositoryFactory(mongoOperations);
return factory.getRepository(repositoryType);
return factory.getRepository(repositoryType, customImplementation);
}
}

View File

@@ -40,6 +40,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport;
* CDI extension to export Mongo repositories.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class MongoRepositoryExtension extends CdiRepositoryExtensionSupport {
@@ -109,7 +110,10 @@ public class MongoRepositoryExtension extends CdiRepositoryExtensionSupport {
MongoOperations.class.getName(), qualifiers));
}
Bean<?> customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers);
// Construct and return the repository bean.
return new MongoRepositoryBean<T>(mongoOperations, qualifiers, repositoryType, beanManager);
return new MongoRepositoryBean<T>(mongoOperations, qualifiers, repositoryType, beanManager,
customImplementationBean);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import org.springframework.data.mongodb.repository.Person;
* Integration tests for {@link MongoRepositoryExtension}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class CdiExtensionIntegrationTests {
@@ -61,4 +62,15 @@ public class CdiExtensionIntegrationTests {
assertThat(result, is(notNullValue()));
assertThat(repository.findOne(person.getId()).getId(), is(result.getId()));
}
/**
* @see DATAMONGO-1017
*/
@Test
public void returnOneFromCustomImpl() {
RepositoryClient repositoryConsumer = container.getInstance(RepositoryClient.class);
assertThat(repositoryConsumer.getSamplePersonRepository().returnOne(), is(1));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,10 +19,12 @@ import javax.inject.Inject;
/**
* @author Oliver Gierke
* @author Mark Paluch
*/
class RepositoryClient {
@Inject CdiPersonRepository repository;
@Inject SamplePersonRepository samplePersonRepository;
/**
* @return the repository
@@ -30,4 +32,8 @@ class RepositoryClient {
public CdiPersonRepository getRepository() {
return repository;
}
public SamplePersonRepository getSamplePersonRepository() {
return samplePersonRepository;
}
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.cdi;
import org.springframework.data.mongodb.core.Person;
import org.springframework.data.repository.Repository;
/**
* @author Mark Paluch
* @see DATAMONGO-1017
*/
public interface SamplePersonRepository extends Repository<Person, Long>, SamplePersonRepositoryCustom {}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.cdi;
/**
* @see DATAMONGO-1017
* @author Mark Paluch
*/
interface SamplePersonRepositoryCustom {
int returnOne();
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.cdi;
/**
* @see DATAMONGO-1017
* @author Mark Paluch
*/
class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom {
@Override
public int returnOne() {
return 1;
}
}