From f718117cd93a804871e6b2804ef4b2e424e38cce Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 12 Mar 2018 16:23:14 +0100 Subject: [PATCH] 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. --- .../jpa/repository/cdi/JpaRepositoryBean.java | 9 ++---- .../cdi/CdiExtensionIntegrationTests.java | 9 +++++- .../QualifiedCustomizedUserRepository.java | 3 +- .../jpa/repository/cdi/QualifiedFragment.java | 23 +++++++++++++++ .../repository/cdi/QualifiedFragmentBean.java | 28 +++++++++++++++++++ .../repository/cdi/RepositoryConsumer.java | 6 +++- 6 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragment.java create mode 100644 src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragmentBean.java diff --git a/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java b/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java index bae13dad5..6b409c94c 100644 --- a/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java +++ b/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java @@ -62,16 +62,13 @@ class JpaRepositoryBean extends CdiRepositoryBean { /* * (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 creationalContext, Class repositoryType, Optional customImplementation) { + protected T create(CreationalContext creationalContext, Class 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); } } diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java index 05934610f..e75c52d23 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java @@ -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)); } } diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedCustomizedUserRepository.java b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedCustomizedUserRepository.java index 698a3dca3..9765efbf7 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedCustomizedUserRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedCustomizedUserRepository.java @@ -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, - QualifiedCustomizedUserRepositoryCustom { + QualifiedCustomizedUserRepositoryCustom, QualifiedFragment { } diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragment.java b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragment.java new file mode 100644 index 000000000..60c9a8fa7 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragment.java @@ -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(); +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragmentBean.java b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragmentBean.java new file mode 100644 index 000000000..2ba82e90d --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedFragmentBean.java @@ -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; + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java b/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java index 63290396a..3a333ee4b 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java @@ -43,7 +43,11 @@ class RepositoryConsumer { return samplePersonRepository.returnOne(); } - public void doSomethonOnUserDB() { + public void doSomethingOnUserDB() { qualifiedCustomizedUserRepository.doSomething(); } + + public int returnOneUserDB() { + return qualifiedCustomizedUserRepository.returnOne(); + } }