DATAJPA-1287 - Export composable repositories via CDI.

We now export composable repositories through our CDI extension. Repositories can now be customized either by a single custom implementation (as it was before) and by providing fragment interfaces along their fragment implementation.

This change aligns CDI support with the existing RepositoryFactory support we provide within a Spring application context.
This commit is contained in:
Mark Paluch
2018-03-12 16:23:14 +01:00
parent 6b9fcf2604
commit f718117cd9
6 changed files with 68 additions and 10 deletions

View File

@@ -62,16 +62,13 @@ class JpaRepositoryBean<T> extends CdiRepositoryBean<T> {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class, java.util.Optional)
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class)
*/
@Override
public T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Optional<Object> customImplementation) {
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
// Get an instance from the associated entity manager bean.
EntityManager entityManager = getDependencyInstance(entityManagerBean, EntityManager.class);
// Create the JPA repository instance and return it.
JpaRepositoryFactory factory = new JpaRepositoryFactory(entityManager);
return customImplementation.isPresent() ? factory.getRepository(repositoryType, customImplementation.get()) : factory.getRepository(repositoryType);
return create(() -> new JpaRepositoryFactory(entityManager), repositoryType);
}
}

View File

@@ -85,6 +85,13 @@ public class CdiExtensionIntegrationTests {
public void useQualifiedCustomizedUserRepo() {
RepositoryConsumer repositoryConsumer = container.select(RepositoryConsumer.class).get();
repositoryConsumer.doSomethonOnUserDB();
repositoryConsumer.doSomethingOnUserDB();
}
@Test // DATAJPA-1287
public void useQualifiedFragmentUserRepo() {
RepositoryConsumer repositoryConsumer = container.select(RepositoryConsumer.class).get();
assertThat(repositoryConsumer.returnOneUserDB(), is(1));
}
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.cdi;
import org.springframework.data.jpa.domain.sample.User;
@@ -24,6 +23,6 @@ import org.springframework.data.repository.Repository;
*/
@UserDB
public interface QualifiedCustomizedUserRepository extends Repository<User, Long>,
QualifiedCustomizedUserRepositoryCustom {
QualifiedCustomizedUserRepositoryCustom, QualifiedFragment {
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2018 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.jpa.repository.cdi;
/**
* @author Mark Paluch
*/
public interface QualifiedFragment {
int returnOne();
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2018 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.jpa.repository.cdi;
/**
* @author Mark Paluch
*/
@UserDB
public class QualifiedFragmentBean implements QualifiedFragment {
@Override
public int returnOne() {
return 1;
}
}

View File

@@ -43,7 +43,11 @@ class RepositoryConsumer {
return samplePersonRepository.returnOne();
}
public void doSomethonOnUserDB() {
public void doSomethingOnUserDB() {
qualifiedCustomizedUserRepository.doSomething();
}
public int returnOneUserDB() {
return qualifiedCustomizedUserRepository.returnOne();
}
}