DATAJPA-584 - Add support for custom implementations in CDI.
Adapted to API changes in Spring Data Commons. Added test cases to make sure the lookup of custom repository implementations works. Original pull request: #105.
This commit is contained in:
committed by
Oliver Gierke
parent
bdedb0fcbe
commit
cd34005349
@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Dirk Mahler
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @param <T> The type of the repository.
|
||||
*/
|
||||
class JpaRepositoryBean<T> extends CdiRepositoryBean<T> {
|
||||
@@ -45,27 +46,29 @@ class JpaRepositoryBean<T> extends CdiRepositoryBean<T> {
|
||||
* @param entityManagerBean must not be {@literal null}.
|
||||
* @param qualifiers must not be {@literal null}.
|
||||
* @param repositoryType must not be {@literal null}.
|
||||
* @param customImplementationBean can be {@literal null}.
|
||||
*/
|
||||
JpaRepositoryBean(BeanManager beanManager, Bean<EntityManager> entityManagerBean, Set<Annotation> qualifiers,
|
||||
Class<T> repositoryType) {
|
||||
Class<T> repositoryType, Bean<?> customImplementationBean) {
|
||||
|
||||
super(qualifiers, repositoryType, beanManager);
|
||||
super(qualifiers, repositoryType, beanManager, customImplementationBean);
|
||||
|
||||
Assert.notNull(entityManagerBean);
|
||||
this.entityManagerBean = entityManagerBean;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.context.spi.Contextual#create(javax.enterprise .context.spi.CreationalContext)
|
||||
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class, java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
|
||||
public T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Object customImplementation) {
|
||||
|
||||
// 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 factory.getRepository(repositoryType);
|
||||
return factory.getRepository(repositoryType, customImplementation);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport;
|
||||
*
|
||||
* @author Dirk Mahler
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class JpaRepositoryExtension extends CdiRepositoryExtensionSupport {
|
||||
|
||||
@@ -57,7 +58,7 @@ public class JpaRepositoryExtension extends CdiRepositoryExtensionSupport {
|
||||
* later association with corresponding repository beans.
|
||||
*
|
||||
* @param <X> The type.
|
||||
* @param processAnnotatedType The annotated type as defined by CDI.
|
||||
* @param processBean The annotated type as defined by CDI.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
<X> void processBean(@Observes ProcessBean<X> processBean) {
|
||||
@@ -118,7 +119,10 @@ public class JpaRepositoryExtension extends CdiRepositoryExtensionSupport {
|
||||
EntityManager.class.getName(), qualifiers));
|
||||
}
|
||||
|
||||
Bean<?> customImplementationBean = getCustomImplementationBean(repositoryType, beanManager, qualifiers);
|
||||
|
||||
// Construct and return the repository bean.
|
||||
return new JpaRepositoryBean<T>(beanManager, entityManagerBean, qualifiers, repositoryType);
|
||||
return new JpaRepositoryBean<T>(beanManager, entityManagerBean, qualifiers, repositoryType,
|
||||
customImplementationBean);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -35,6 +35,7 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
* @author Dirk Mahler
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class CdiExtensionIntegrationTests {
|
||||
|
||||
@@ -73,4 +74,24 @@ public class CdiExtensionIntegrationTests {
|
||||
repositoryConsumer.save(person);
|
||||
repositoryConsumer.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
*/
|
||||
@Test
|
||||
public void returnOneFromCustomImpl() {
|
||||
|
||||
RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class);
|
||||
assertThat(repositoryConsumer.returnOne(), is(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
*/
|
||||
@Test
|
||||
public void useQualifiedCustomizedUserRepo() {
|
||||
|
||||
RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class);
|
||||
repositoryConsumer.doSomethonOnUserDB();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.jpa.repository.cdi;
|
||||
|
||||
import org.springframework.data.repository.cdi.CdiRepositoryConfiguration;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@UserDB
|
||||
public class QualifiedCustomizedCdiConfiguration implements CdiRepositoryConfiguration {
|
||||
|
||||
@Override
|
||||
public String getRepositoryImplementationPostfix() {
|
||||
return "Bean";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.jpa.repository.cdi;
|
||||
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@UserDB
|
||||
public interface QualifiedCustomizedUserRepository extends Repository<User, Long>,
|
||||
QualifiedCustomizedUserRepositoryCustom {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.jpa.repository.cdi;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@UserDB
|
||||
public class QualifiedCustomizedUserRepositoryBean implements QualifiedCustomizedUserRepositoryCustom {
|
||||
|
||||
@Override
|
||||
public void doSomething() {}
|
||||
}
|
||||
@@ -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.jpa.repository.cdi;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface QualifiedCustomizedUserRepositoryCustom {
|
||||
|
||||
void doSomething();
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -20,15 +20,29 @@ import javax.enterprise.inject.Produces;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class QualifiedEntityManagerProducer {
|
||||
|
||||
@Produces
|
||||
@PersonDB
|
||||
public EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) {
|
||||
public EntityManager createPersonDBEntityManager(EntityManagerFactory entityManagerFactory) {
|
||||
return entityManagerFactory.createEntityManager();
|
||||
}
|
||||
|
||||
public void close(@Disposes @PersonDB EntityManager entityManager) {
|
||||
public void closePersonDB(@Disposes @PersonDB EntityManager entityManager) {
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
@Produces
|
||||
@UserDB
|
||||
public EntityManager createUserDBEntityManager(EntityManagerFactory entityManagerFactory) {
|
||||
return entityManagerFactory.createEntityManager();
|
||||
}
|
||||
|
||||
public void closeUserDB(@Disposes @UserDB EntityManager entityManager) {
|
||||
entityManager.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -17,15 +17,17 @@ package org.springframework.data.jpa.repository.cdi;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Transactional
|
||||
class RepositoryConsumer {
|
||||
|
||||
@Inject
|
||||
private PersonRepository unqualifiedRepo;
|
||||
|
||||
@Inject
|
||||
@PersonDB
|
||||
private PersonRepository qualifiedRepo;
|
||||
@Inject PersonRepository unqualifiedRepo;
|
||||
@Inject @PersonDB PersonRepository qualifiedRepo;
|
||||
@Inject SamplePersonRepository samplePersonRepository;
|
||||
@Inject @UserDB QualifiedCustomizedUserRepository qualifiedCustomizedUserRepository;
|
||||
|
||||
public void findAll() {
|
||||
unqualifiedRepo.findAll();
|
||||
@@ -36,4 +38,12 @@ class RepositoryConsumer {
|
||||
unqualifiedRepo.save(person);
|
||||
qualifiedRepo.save(person);
|
||||
}
|
||||
|
||||
public int returnOne() {
|
||||
return samplePersonRepository.returnOne();
|
||||
}
|
||||
|
||||
public void doSomethonOnUserDB() {
|
||||
qualifiedCustomizedUserRepository.doSomething();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.jpa.repository.cdi;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface SamplePersonRepository extends Repository<Person, Long>, SamplePersonRepositoryCustom {}
|
||||
@@ -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.jpa.repository.cdi;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
interface SamplePersonRepositoryCustom {
|
||||
|
||||
int returnOne();
|
||||
}
|
||||
@@ -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.jpa.repository.cdi;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class SamplePersonRepositoryImpl implements SamplePersonRepositoryCustom {
|
||||
|
||||
@Override
|
||||
public int returnOne() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.jpa.repository.cdi;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-584
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
|
||||
@interface UserDB {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user