DATALDAP-2- Support repositoryBaseClass and considerNestedRepositories in @EnableLdapRepositories annotation.

We now support nested repository interfaces and customization via EnableLdapRepositories.repositoryBaseClass.

  @Configuration
  @EnableLdapRepositories(considerNestedRepositories = true, repositoryBaseClass = CustomizedLdapRepository.class)
  public class Config {
    // …
  }
This commit is contained in:
Mark Paluch
2016-12-06 11:11:49 +01:00
parent ba9f653c95
commit 1e35b624b7
3 changed files with 122 additions and 33 deletions

View File

@@ -24,15 +24,16 @@ import java.lang.annotation.Target;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Import;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean;
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
/**
* Annotation to activate Ldap 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.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
* @author Mark Paluch
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@@ -43,7 +44,8 @@ public @interface EnableLdapRepositories {
/**
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
* {@code @EnableLdapRepositories("org.my.pkg")} instead of {@code @EnableLdapRepositories(basePackages="org.my.pkg")}.
* {@code @EnableLdapRepositories("org.my.pkg")} instead of
* {@code @EnableLdapRepositories(basePackages="org.my.pkg")}.
*/
String[] value() default {};
@@ -78,7 +80,7 @@ public @interface EnableLdapRepositories {
*
* @return
*/
String repositoryImplementationPostfix() default "";
String repositoryImplementationPostfix() default "Impl";
/**
* Configures the location of where to find the Spring Data named queries properties file. Will default to
@@ -89,7 +91,8 @@ public @interface EnableLdapRepositories {
String namedQueriesLocation() default "";
/**
* Returns the key of the {@link org.springframework.data.repository.query.QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
* Returns the key of the {@link org.springframework.data.repository.query.QueryLookupStrategy} to be used for lookup
* queries for query methods. Defaults to
* {@link org.springframework.data.repository.query.QueryLookupStrategy.Key#CREATE_IF_NOT_FOUND}.
*
* @return
@@ -97,17 +100,31 @@ public @interface EnableLdapRepositories {
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
/**
* Returns the {@link org.springframework.beans.factory.FactoryBean} class to be used for each repository instance. Defaults to
* {@link org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean}.
* Returns the {@link org.springframework.beans.factory.FactoryBean} class to be used for each repository instance.
* Defaults to {@link org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean}.
*
* @return
*/
Class<?> repositoryFactoryBeanClass() default LdapRepositoryFactoryBean.class;
/**
* Configures the name of the {@link org.springframework.ldap.core.LdapTemplate} bean to be used with the repositories detected.
* Configure the repository base class to be used to create repository proxies for this particular configuration.
*
* @return
*/
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
/**
* Configures the name of the {@link org.springframework.ldap.core.LdapTemplate} bean to be used with the repositories
* detected.
*
* @return
*/
String ldapTemplateRef() default "ldapTemplate";
/**
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
* repositories infrastructure.
*/
boolean considerNestedRepositories() default false;
}

View File

@@ -20,6 +20,9 @@ import static org.springframework.data.querydsl.QueryDslUtils.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.springframework.data.ldap.repository.query.AnnotatedLdapRepositoryQuery;
import org.springframework.data.ldap.repository.query.LdapQueryMethod;
import org.springframework.data.ldap.repository.query.PartTreeLdapRepositoryQuery;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.core.EntityInformation;
@@ -32,17 +35,13 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.data.ldap.repository.query.AnnotatedLdapRepositoryQuery;
import org.springframework.data.ldap.repository.query.LdapQueryMethod;
import org.springframework.data.ldap.repository.query.PartTreeLdapRepositoryQuery;
import org.springframework.data.ldap.repository.support.SimpleLdapRepository;
/**
* Factory to create {@link org.springframework.data.ldap.repository.LdapRepository} instances.
*
* @author Mattias Hellborg Arthursson
* @author Eddu Melendez
* @since 2.0
* @author Mark Paluch
*/
public class LdapRepositoryFactory extends RepositoryFactorySupport {
@@ -57,33 +56,28 @@ public class LdapRepositoryFactory extends RepositoryFactorySupport {
return null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object getTargetRepository(RepositoryMetadata metadata) {
if (!isQueryDslRepository(metadata.getRepositoryInterface())) {
return new org.springframework.data.ldap.repository.support.SimpleLdapRepository(ldapOperations,
ldapOperations.getObjectDirectoryMapper(), metadata.getDomainType());
}
return new QueryDslLdapRepository(ldapOperations, ldapOperations.getObjectDirectoryMapper(),
metadata.getDomainType());
}
protected Object getTargetRepository(RepositoryInformation metadata) {
return getTargetRepository((RepositoryMetadata) metadata);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getRepositoryBaseClass(org.springframework.data.repository.core.RepositoryMetadata)
*/
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return isQueryDslRepository(metadata.getRepositoryInterface()) ? QueryDslLdapRepository.class
: SimpleLdapRepository.class;
}
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryInformation)
*/
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
if (!isQueryDslRepository(metadata.getRepositoryInterface())) {
return SimpleLdapRepository.class;
} else {
return QueryDslLdapRepository.class;
}
protected Object getTargetRepository(RepositoryInformation information) {
return getTargetRepositoryViaReflection(information, ldapOperations, ldapOperations.getObjectDirectoryMapper(),
information.getDomainType());
}
@Override
@@ -101,6 +95,7 @@ public class LdapRepositoryFactory extends RepositoryFactorySupport {
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
NamedQueries namedQueries) {
LdapQueryMethod queryMethod = new LdapQueryMethod(method, metadata, factory);
Class<?> domainType = metadata.getDomainType();