DATAGRAPH-500 - Add support for custom implementations in CDI repositories.

Original pull request: #198.
This commit is contained in:
Mark Paluch
2014-08-08 08:05:48 +02:00
committed by Oliver Gierke
parent 144e4fb6cb
commit 8a9b3d6471
7 changed files with 109 additions and 5 deletions

View File

@@ -32,6 +32,7 @@ import java.util.Set;
* {@link org.springframework.data.repository.cdi.CdiRepositoryBean} to create Neo4j repository instances via CDI.
*
* @author Nicki Watt
* @author Mark Paluch
*/
public class Neo4jCdiRepositoryBean<T> extends CdiRepositoryBean<T> {
@@ -44,11 +45,13 @@ public class Neo4jCdiRepositoryBean<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 Neo4jCdiRepositoryBean(Bean<GraphDatabase> graphDatabase,
Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager, Bean<?> customImplementationBean) {
super(qualifiers, repositoryType, beanManager);
super(qualifiers, repositoryType, beanManager, customImplementationBean);
this.graphDatabase = graphDatabase;
}
@@ -58,12 +61,12 @@ public class Neo4jCdiRepositoryBean<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) {
Neo4jMappingContext neo4jMapCtx = new Neo4jMappingContext();
Neo4jTemplate neo4jTemplate = new Neo4jTemplate(getDependencyInstance(graphDatabase, GraphDatabase.class));
GraphRepositoryFactory factory = new GraphRepositoryFactory(neo4jTemplate, neo4jMapCtx);
return factory.getRepository(repositoryType);
return factory.getRepository(repositoryType, customImplementation);
}
}

View File

@@ -42,6 +42,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport;
*
* @author Nicki Watt
* @author Oliver Gierke
* @author Mark Paluch
*/
public class Neo4jCdiRepositoryExtension extends CdiRepositoryExtensionSupport {
@@ -108,7 +109,9 @@ public class Neo4jCdiRepositoryExtension extends CdiRepositoryExtensionSupport {
Neo4jMappingContext.class.getName(), qualifiers));
}
Bean<?> customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers);
return new Neo4jCdiRepositoryBean<T>(graphDatabase, qualifiers, repositoryType,
beanManager);
beanManager, customImplementationBean);
}
}

View File

@@ -141,4 +141,19 @@ public class CdiExtensionIntegrationTests {
}
}
/**
* @see DATAGRAPH-500
*/
@Test
public void returnOneFromCustomImpl() {
GraphDatabase database = container.getInstance(GraphDatabase.class);
try (Transaction tx = database.beginTx()) {
RepositoryClient client = container.getInstance(RepositoryClient.class);
assertThat(client.samplePersonRepository.returnOne(), is(1));
}
}
}

View File

@@ -26,4 +26,5 @@ class RepositoryClient {
@Inject CdiPersonRepository repository;
@Inject CdiPersonRepository2 repository2;
@Inject CdiPersonRepository3 repository3;
@Inject SamplePersonRepository samplePersonRepository;
}

View File

@@ -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.neo4j.repository.cdi;
import org.springframework.data.neo4j.model.Person;
import org.springframework.data.repository.Repository;
/**
* @author Mark Paluch
* @see DATAGRAPH-500
*/
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.neo4j.repository.cdi;
/**
* @see DATAGRAPH-500
* @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.neo4j.repository.cdi;
/**
* @see DATAGRAPH-500
* @author Mark Paluch
*/
class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom {
@Override
public int returnOne() {
return 1;
}
}