Skip to content

DATAMONGO-1017 Add support for custom implementations in CDI repositories #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -31,6 +31,7 @@
* {@link CdiRepositoryBean} to create Mongo repository instances.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {

Expand All @@ -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;
Expand All @@ -58,11 +61,11 @@ public MongoRepositoryBean(Bean<MongoOperations> operations, Set<Annotation> qua
* @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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* CDI extension to export Mongo repositories.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class MongoRepositoryExtension extends CdiRepositoryExtensionSupport {

Expand Down Expand Up @@ -109,7 +110,9 @@ private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, S
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);
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,6 +29,7 @@
* Integration tests for {@link MongoRepositoryExtension}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class CdiExtensionIntegrationTests {

Expand Down Expand Up @@ -61,4 +62,16 @@ public void bootstrapsRepositoryCorrectly() {
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));
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,10 +24,17 @@ class RepositoryClient {

@Inject CdiPersonRepository repository;

@Inject SamplePersonRepository samplePersonRepository;

/**
* @return the repository
*/
public CdiPersonRepository getRepository() {
return repository;
}

public SamplePersonRepository getSamplePersonRepository() {
return samplePersonRepository;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 {

}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}