Initial support for JavaConfig repositories

This commit is contained in:
Michael Nitschinger
2013-05-29 12:50:45 +02:00
parent 74e9a86f82
commit ffbff4a416
3 changed files with 139 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package com.couchbase.spring.repository.config;
import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import java.lang.annotation.Annotation;
public class CouchbaseRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableCouchbaseRepositories.class;
}
@Override
protected RepositoryConfigurationExtension getExtension() {
return new CouchbaseRepositoryConfigurationExtension();
}
}

View File

@@ -0,0 +1,37 @@
package com.couchbase.spring.repository.config;
import com.couchbase.spring.repository.support.CouchbaseRepositoryFactoryBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.data.config.ParsingUtils;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
import org.springframework.data.repository.config.XmlRepositoryConfigurationSource;
import org.w3c.dom.Element;
public class CouchbaseRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
private static final String COUCHBASE_TEMPLATE_REF = "couchbase-template-ref";
@Override
protected String getModulePrefix() {
return "couchbase";
}
public String getRepositoryFactoryClassName() {
return CouchbaseRepositoryFactoryBean.class.getName();
}
@Override
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
Element element = config.getElement();
ParsingUtils.setPropertyReference(builder, element, COUCHBASE_TEMPLATE_REF, "couchbaseOperations");
}
@Override
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
AnnotationAttributes attributes = config.getAttributes();
builder.addPropertyReference("couchbaseOperations", attributes.getString("couchbaseTemplateRef"));
}
}

View File

@@ -0,0 +1,81 @@
package com.couchbase.spring.repository.config;
import com.couchbase.spring.repository.support.CouchbaseRepositoryFactoryBean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ComponentScan.Filter;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to activate Couchbase repositories. If no base package is configured through either {@link #value()},
* {@link #basePackages()} or {@link #basePackageClasses()} it will trigger scanning of the package of annotated class.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(CouchbaseRepositoriesRegistrar.class)
public @interface EnableCouchbaseRepositories {
/**
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
* {@code @EnableCouchbaseRepositories("org.my.pkg")} instead of {@code @EnableCouchbaseRepositories(basePackages="org.my.pkg")}.
*/
String[] value() default {};
/**
* Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
* attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
*/
String[] basePackages() default {};
/**
* Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
* package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
* each package that serves no purpose other than being referenced by this attribute.
*/
Class<?>[] basePackageClasses() default {};
/**
* Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
* everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
*/
Filter[] includeFilters() default {};
/**
* Specifies which types are not eligible for component scanning.
*/
Filter[] excludeFilters() default {};
/**
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
* for {@code PersonRepositoryImpl}.
*
* @return
*/
String repositoryImplementationPostfix() default "";
/**
* Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
* {@link CouchbaseRepositoryFactoryBean}.
*
* @return
*/
Class<?> repositoryFactoryBeanClass() default CouchbaseRepositoryFactoryBean.class;
/**
* Configures the name of the {@link CouchbaseTemplate} bean to be used with the repositories detected.
*
* @return
*/
String couchbaseTemplateRef() default "couchbaseTemplate";
}