Class RepositoryFactorySupport

java.lang.Object
org.springframework.data.repository.core.support.RepositoryFactorySupport
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware
Direct Known Subclasses:
ReactiveRepositoryFactorySupport

public abstract class RepositoryFactorySupport extends Object implements org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware
Factory bean to create instances of a given repository interface. Creates a proxy implementing the configured repository interface and apply an advice handing the control to the QueryExecutorMethodInterceptor. Query detection strategy can be configured by setting QueryLookupStrategy.Key.
Author:
Oliver Gierke, Mark Paluch, Christoph Strobl, Jens Schauder, John Blum, Johannes Englmeier
  • Constructor Details

    • RepositoryFactorySupport

      public RepositoryFactorySupport()
  • Method Details

    • setQueryLookupStrategyKey

      public void setQueryLookupStrategyKey(QueryLookupStrategy.Key key)
      Sets the strategy of how to lookup a query to execute finders.
      Parameters:
      key -
    • setNamedQueries

      public void setNamedQueries(NamedQueries namedQueries)
      Configures a NamedQueries instance to be handed to the QueryLookupStrategy for query creation.
      Parameters:
      namedQueries - the namedQueries to set
    • setBeanClassLoader

      public void setBeanClassLoader(ClassLoader classLoader)
      Specified by:
      setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware
    • setBeanFactory

      public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
      Throws:
      org.springframework.beans.BeansException
    • setEvaluationContextProvider

      public void setEvaluationContextProvider(QueryMethodEvaluationContextProvider evaluationContextProvider)
      Sets the QueryMethodEvaluationContextProvider to be used to evaluate SpEL expressions in manually defined queries.
      Parameters:
      evaluationContextProvider - can be null, defaults to QueryMethodEvaluationContextProvider.DEFAULT.
    • setRepositoryBaseClass

      public void setRepositoryBaseClass(Class<?> repositoryBaseClass)
      Configures the repository base class to use when creating the repository proxy. If not set, the factory will use the type returned by getRepositoryBaseClass(RepositoryMetadata) by default.
      Parameters:
      repositoryBaseClass - the repository base class to back the repository proxy, can be null.
      Since:
      1.11
    • addQueryCreationListener

      public void addQueryCreationListener(QueryCreationListener<?> listener)
      Adds a QueryCreationListener to the factory to plug in functionality triggered right after creation of RepositoryQuery instances.
      Parameters:
      listener -
    • addInvocationListener

      public void addInvocationListener(RepositoryMethodInvocationListener listener)
      Adds a RepositoryMethodInvocationListener to the factory to plug in functionality triggered right after running query methods and fragment methods.
      Parameters:
      listener -
      Since:
      2.4
    • addRepositoryProxyPostProcessor

      public void addRepositoryProxyPostProcessor(RepositoryProxyPostProcessor processor)
      Adds RepositoryProxyPostProcessors to the factory to allow manipulation of the ProxyFactory before the proxy gets created. Note that the QueryExecutorMethodInterceptor will be added to the proxy after the RepositoryProxyPostProcessors are considered.
      Parameters:
      processor -
    • getRepositoryFragments

      protected RepositoryComposition.RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata)
      Creates RepositoryComposition.RepositoryFragments based on RepositoryMetadata to add repository-specific extensions.
      Parameters:
      metadata -
      Returns:
    • getRepository

      public <T> T getRepository(Class<T> repositoryInterface)
      Returns a repository instance for the given interface.
      Parameters:
      repositoryInterface - must not be null.
      Returns:
    • getRepository

      public <T> T getRepository(Class<T> repositoryInterface, Object customImplementation)
      Returns a repository instance for the given interface backed by a single instance providing implementation logic for custom logic. For more advanced composition needs use getRepository(Class, RepositoryFragments).
      Parameters:
      repositoryInterface - must not be null.
      customImplementation - must not be null.
      Returns:
    • getRepository

      public <T> T getRepository(Class<T> repositoryInterface, RepositoryComposition.RepositoryFragments fragments)
      Returns a repository instance for the given interface backed by an instance providing implementation logic for custom logic.
      Parameters:
      repositoryInterface - must not be null.
      fragments - must not be null.
      Returns:
      Since:
      2.0
    • getProjectionFactory

      protected ProjectionFactory getProjectionFactory(ClassLoader classLoader, org.springframework.beans.factory.BeanFactory beanFactory)
      Returns the ProjectionFactory to be used with the repository instances created.
      Parameters:
      classLoader - will never be null.
      beanFactory - will never be null.
      Returns:
      will never be null.
    • getRepositoryMetadata

      protected RepositoryMetadata getRepositoryMetadata(Class<?> repositoryInterface)
      Returns the RepositoryMetadata for the given repository interface.
      Parameters:
      repositoryInterface - will never be null.
      Returns:
    • getRepositoryInformation

      protected RepositoryInformation getRepositoryInformation(RepositoryMetadata metadata, RepositoryComposition.RepositoryFragments fragments)
      Parameters:
      metadata - must not be null.
      fragments - must not be null.
      Returns:
      will never be null.
    • getQueryMethods

      protected List<QueryMethod> getQueryMethods()
    • getProjectionFactory

      protected ProjectionFactory getProjectionFactory()
      Returns a ProjectionFactory instance.
      Returns:
      Since:
      2.6
    • getEntityInformation

      public abstract <T, ID> EntityInformation<T,ID> getEntityInformation(Class<T> domainClass)
      Returns the EntityInformation for the given domain class.
      Type Parameters:
      T - the entity type
      ID - the id type
      Parameters:
      domainClass -
      Returns:
    • getTargetRepository

      protected abstract Object getTargetRepository(RepositoryInformation metadata)
      Create a repository instance as backing for the query proxy.
      Parameters:
      metadata -
      Returns:
    • getRepositoryBaseClass

      protected abstract Class<?> getRepositoryBaseClass(RepositoryMetadata metadata)
      Returns the base class backing the actual repository instance. Make sure getTargetRepository(RepositoryInformation) returns an instance of this class.
      Parameters:
      metadata -
      Returns:
    • getQueryLookupStrategy

      protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLookupStrategy.Key key, QueryMethodEvaluationContextProvider evaluationContextProvider)
      Parameters:
      key - can be null.
      evaluationContextProvider - will never be null.
      Returns:
      the QueryLookupStrategy to use or null if no queries should be looked up.
      Since:
      1.9
    • validate

      protected void validate(RepositoryMetadata repositoryMetadata)
    • getTargetRepositoryViaReflection

      protected final <R> R getTargetRepositoryViaReflection(RepositoryInformation information, Object... constructorArguments)
      Creates a repository of the repository base class defined in the given RepositoryInformation using reflection.
      Parameters:
      information -
      constructorArguments -
      Returns:
    • getTargetRepositoryViaReflection

      @Deprecated protected final <R> R getTargetRepositoryViaReflection(Class<?> baseClass, Object... constructorArguments)
      Deprecated.
      since 2.6 because it has a misleading name. Use instantiateClass(Class, Object...) instead.
      Creates a repository of the repository base class defined in the given RepositoryInformation using reflection.
      Parameters:
      baseClass -
      constructorArguments -
      Returns:
    • instantiateClass

      protected final <R> R instantiateClass(Class<?> baseClass, Object... constructorArguments)
      Convenience method to instantiate a class using the given constructorArguments by looking up a matching constructor.

      Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor, and supports Kotlin classes with optional parameters and default values.

      Parameters:
      baseClass -
      constructorArguments -
      Returns:
      Since:
      2.6