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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds aRepositoryMethodInvocationListenerto the factory to plug in functionality triggered right after runningquery methodsandfragment methods.voidaddQueryCreationListener(QueryCreationListener<?> listener) Adds aQueryCreationListenerto the factory to plug in functionality triggered right after creation ofRepositoryQueryinstances.voidAddsRepositoryProxyPostProcessors to the factory to allow manipulation of theProxyFactorybefore the proxy gets created.abstract <T,ID> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) Returns theEntityInformationfor the given domain class.protected ProjectionFactoryReturns aProjectionFactoryinstance.protected ProjectionFactorygetProjectionFactory(ClassLoader classLoader, org.springframework.beans.factory.BeanFactory beanFactory) Returns theProjectionFactoryto be used with the repository instances created.protected Optional<QueryLookupStrategy>getQueryLookupStrategy(QueryLookupStrategy.Key key, QueryMethodEvaluationContextProvider evaluationContextProvider) Returns theQueryLookupStrategyfor the givenQueryLookupStrategy.KeyandQueryMethodEvaluationContextProvider.protected List<QueryMethod><T> TgetRepository(Class<T> repositoryInterface) Returns a repository instance for the given interface.<T> TgetRepository(Class<T> repositoryInterface, Object customImplementation) Returns a repository instance for the given interface backed by a single instance providing implementation logic for custom logic.<T> TgetRepository(Class<T> repositoryInterface, RepositoryComposition.RepositoryFragments fragments) Returns a repository instance for the given interface backed by an instance providing implementation logic for custom logic.protected abstract Class<?>getRepositoryBaseClass(RepositoryMetadata metadata) Returns the base class backing the actual repository instance.getRepositoryFragments(RepositoryMetadata metadata) CreatesRepositoryComposition.RepositoryFragmentsbased onRepositoryMetadatato add repository-specific extensions.protected RepositoryInformationgetRepositoryInformation(RepositoryMetadata metadata, RepositoryComposition.RepositoryFragments fragments) Returns theRepositoryInformationfor the givenRepositoryMetadataand customRepositoryComposition.RepositoryFragments.protected RepositoryMetadatagetRepositoryMetadata(Class<?> repositoryInterface) Returns theRepositoryMetadatafor the given repository interface.protected abstract ObjectgetTargetRepository(RepositoryInformation metadata) Create a repository instance as backing for the query proxy.protected final <R> RgetTargetRepositoryViaReflection(Class<?> baseClass, Object... constructorArguments) Deprecated.since 2.6 because it has a misleading name.protected final <R> RgetTargetRepositoryViaReflection(RepositoryInformation information, Object... constructorArguments) Creates a repository of the repository base class defined in the givenRepositoryInformationusing reflection.protected final <R> RinstantiateClass(Class<?> baseClass, Object... constructorArguments) Convenience method to instantiate a class using the givenconstructorArgumentsby looking up a matching constructor.voidsetBeanClassLoader(ClassLoader classLoader) voidsetBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) voidsetEvaluationContextProvider(QueryMethodEvaluationContextProvider evaluationContextProvider) Sets theQueryMethodEvaluationContextProviderto be used to evaluate SpEL expressions in manually defined queries.voidsetNamedQueries(NamedQueries namedQueries) Configures aNamedQueriesinstance to be handed to theQueryLookupStrategyfor query creation.voidSets the strategy of how to lookup a query to execute finders.voidsetRepositoryBaseClass(Class<?> repositoryBaseClass) Configures the repository base class to use when creating the repository proxy.protected voidvalidate(RepositoryMetadata repositoryMetadata)
-
Constructor Details
-
RepositoryFactorySupport
public RepositoryFactorySupport()
-
-
Method Details
-
setQueryLookupStrategyKey
Sets the strategy of how to lookup a query to execute finders.- Parameters:
key-
-
setNamedQueries
Configures aNamedQueriesinstance to be handed to theQueryLookupStrategyfor query creation.- Parameters:
namedQueries- the namedQueries to set
-
setBeanClassLoader
- Specified by:
setBeanClassLoaderin interfaceorg.springframework.beans.factory.BeanClassLoaderAware
-
setBeanFactory
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException - Specified by:
setBeanFactoryin interfaceorg.springframework.beans.factory.BeanFactoryAware- Throws:
org.springframework.beans.BeansException
-
setEvaluationContextProvider
public void setEvaluationContextProvider(QueryMethodEvaluationContextProvider evaluationContextProvider) Sets theQueryMethodEvaluationContextProviderto be used to evaluate SpEL expressions in manually defined queries.- Parameters:
evaluationContextProvider- can be null, defaults toQueryMethodEvaluationContextProvider.DEFAULT.
-
setRepositoryBaseClass
Configures the repository base class to use when creating the repository proxy. If not set, the factory will use the type returned bygetRepositoryBaseClass(RepositoryMetadata)by default.- Parameters:
repositoryBaseClass- the repository base class to back the repository proxy, can be null.- Since:
- 1.11
-
addQueryCreationListener
Adds aQueryCreationListenerto the factory to plug in functionality triggered right after creation ofRepositoryQueryinstances.- Parameters:
listener-
-
addInvocationListener
Adds aRepositoryMethodInvocationListenerto the factory to plug in functionality triggered right after runningquery methodsandfragment methods.- Parameters:
listener-- Since:
- 2.4
-
addRepositoryProxyPostProcessor
AddsRepositoryProxyPostProcessors to the factory to allow manipulation of theProxyFactorybefore the proxy gets created. Note that theQueryExecutorMethodInterceptorwill be added to the proxy after theRepositoryProxyPostProcessors are considered.- Parameters:
processor-
-
getRepositoryFragments
protected RepositoryComposition.RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) CreatesRepositoryComposition.RepositoryFragmentsbased onRepositoryMetadatato add repository-specific extensions.- Parameters:
metadata-- Returns:
-
getRepository
Returns a repository instance for the given interface.- Parameters:
repositoryInterface- must not be null.- Returns:
-
getRepository
Returns a repository instance for the given interface backed by a single instance providing implementation logic for custom logic. For more advanced composition needs usegetRepository(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 theProjectionFactoryto be used with the repository instances created.- Parameters:
classLoader- will never be null.beanFactory- will never be null.- Returns:
- will never be null.
-
getRepositoryMetadata
Returns theRepositoryMetadatafor the given repository interface.- Parameters:
repositoryInterface- will never be null.- Returns:
-
getRepositoryInformation
protected RepositoryInformation getRepositoryInformation(RepositoryMetadata metadata, RepositoryComposition.RepositoryFragments fragments) Returns theRepositoryInformationfor the givenRepositoryMetadataand customRepositoryComposition.RepositoryFragments.- Parameters:
metadata- must not be null.fragments- must not be null.- Returns:
- will never be null.
-
getQueryMethods
-
getProjectionFactory
Returns aProjectionFactoryinstance.- Returns:
- Since:
- 2.6
-
getEntityInformation
Returns theEntityInformationfor the given domain class.- Type Parameters:
T- the entity typeID- the id type- Parameters:
domainClass-- Returns:
-
getTargetRepository
Create a repository instance as backing for the query proxy.- Parameters:
metadata-- Returns:
-
getRepositoryBaseClass
Returns the base class backing the actual repository instance. Make suregetTargetRepository(RepositoryInformation)returns an instance of this class.- Parameters:
metadata-- Returns:
-
getQueryLookupStrategy
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLookupStrategy.Key key, QueryMethodEvaluationContextProvider evaluationContextProvider) Returns theQueryLookupStrategyfor the givenQueryLookupStrategy.KeyandQueryMethodEvaluationContextProvider.- Parameters:
key- can be null.evaluationContextProvider- will never be null.- Returns:
- the
QueryLookupStrategyto use or null if no queries should be looked up. - Since:
- 1.9
-
validate
-
getTargetRepositoryViaReflection
protected final <R> R getTargetRepositoryViaReflection(RepositoryInformation information, Object... constructorArguments) Creates a repository of the repository base class defined in the givenRepositoryInformationusing 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. UseinstantiateClass(Class, Object...)instead.Creates a repository of the repository base class defined in the givenRepositoryInformationusing reflection.- Parameters:
baseClass-constructorArguments-- Returns:
-
instantiateClass
Convenience method to instantiate a class using the givenconstructorArgumentsby 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
-