DATACOUCH-368 - 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:55:30 +01:00
parent 47afa86e92
commit a46f8160a9
4 changed files with 43 additions and 49 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-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.
@@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.repository.cdi;
/**
* @author Mark Paluch
*/
public interface CdiPersonRepositoryCustom {
public interface CdiPersonFragment {
int returnTwo();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-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.
@@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.repository.cdi;
/**
* @author Mark Paluch
*/
public class CdiPersonRepositoryImpl implements CdiPersonRepositoryCustom {
public class CdiPersonFragmentImpl implements CdiPersonFragment {
@Override
public int returnTwo() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-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.
@@ -13,15 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.repository.cdi;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.User;
/**
* @author Mark Paluch
*/
public interface CdiPersonRepository extends CouchbaseRepository<Person, String>, CdiPersonRepositoryCustom {
public interface CdiPersonRepository extends CouchbaseRepository<Person, String>, CdiPersonFragment {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-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.
@@ -13,15 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.repository.cdi;
import java.lang.annotation.Annotation;
import java.util.Optional;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import java.lang.annotation.Annotation;
import java.util.Optional;
import java.util.Set;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
@@ -33,50 +33,48 @@ import org.springframework.util.Assert;
/**
* A bean which represents a Couchbase repository.
*
* @author Mark Paluch
*/
public class CouchbaseRepositoryBean<T> extends CdiRepositoryBean<T> {
private final Bean<CouchbaseOperations> couchbaseOperationsBean;
private final Bean<CouchbaseOperations> couchbaseOperationsBean;
/**
* Creates a new {@link CouchbaseRepositoryBean}.
*
* @param operations must not be {@literal null}.
* @param qualifiers must not be {@literal null}.
* @param repositoryType must not be {@literal null}.
* @param beanManager must not be {@literal null}.
* @param detector detector for the custom {@link org.springframework.data.repository.Repository} implementations
* {@link org.springframework.data.repository.config.CustomRepositoryImplementationDetector}, can be {@literal null}.
*/
public CouchbaseRepositoryBean(Bean<CouchbaseOperations> operations, Set<Annotation> qualifiers, Class<T> repositoryType,
BeanManager beanManager, CustomRepositoryImplementationDetector detector) {
super(qualifiers, repositoryType, beanManager, Optional.of(detector));
/**
* Creates a new {@link CouchbaseRepositoryBean}.
*
* @param operations must not be {@literal null}.
* @param qualifiers must not be {@literal null}.
* @param repositoryType must not be {@literal null}.
* @param beanManager must not be {@literal null}.
* @param detector detector for the custom {@link org.springframework.data.repository.Repository} implementations
* {@link org.springframework.data.repository.config.CustomRepositoryImplementationDetector}, can be
* {@literal null}.
*/
public CouchbaseRepositoryBean(Bean<CouchbaseOperations> operations, Set<Annotation> qualifiers,
Class<T> repositoryType, BeanManager beanManager, CustomRepositoryImplementationDetector detector) {
super(qualifiers, repositoryType, beanManager, Optional.of(detector));
Assert.notNull(operations, "Cannot create repository with 'null' for CouchbaseOperations.");
this.couchbaseOperationsBean = operations;
}
Assert.notNull(operations, "Cannot create repository with 'null' for CouchbaseOperations.");
this.couchbaseOperationsBean = operations;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class, java.lang.Object)
*/
@Override
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Optional<Object> customImplementation) {
* @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) {
CouchbaseOperations couchbaseOperations = getDependencyInstance(couchbaseOperationsBean, CouchbaseOperations.class);
RepositoryOperationsMapping couchbaseOperationsMapping = new RepositoryOperationsMapping(couchbaseOperations);
IndexManager indexManager = new IndexManager();
CouchbaseOperations couchbaseOperations = getDependencyInstance(couchbaseOperationsBean, CouchbaseOperations.class);
RepositoryOperationsMapping couchbaseOperationsMapping = new RepositoryOperationsMapping(couchbaseOperations);
IndexManager indexManager = new IndexManager();
CouchbaseRepositoryFactory factory =
new CouchbaseRepositoryFactory(couchbaseOperationsMapping, indexManager);
return create(() -> new CouchbaseRepositoryFactory(couchbaseOperationsMapping, indexManager), repositoryType);
}
return customImplementation.map(o -> factory.getRepository(repositoryType, o))
.orElseGet(() -> factory.getRepository(repositoryType));
}
@Override
public Class<? extends Annotation> getScope() {
return couchbaseOperationsBean.getScope();
}
@Override
public Class<? extends Annotation> getScope() {
return couchbaseOperationsBean.getScope();
}
}