Class RepositoryFactorySupport
java.lang.Object
org.springframework.data.repository.core.support.RepositoryFactorySupport
- All Implemented Interfaces:
Aware,BeanClassLoaderAware,BeanFactoryAware,EnvironmentAware,EnvironmentCapable
- Direct Known Subclasses:
ReactiveRepositoryFactorySupport
public abstract class RepositoryFactorySupport
extends Object
implements BeanClassLoaderAware, BeanFactoryAware, EnvironmentAware, EnvironmentCapable
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, BeanFactory beanFactory) Returns theProjectionFactoryto be used with the repository instances created.protected Optional<QueryLookupStrategy>getQueryLookupStrategy(QueryLookupStrategy.Key key, QueryMethodEvaluationContextProvider evaluationContextProvider) Deprecated.protected Optional<QueryLookupStrategy>getQueryLookupStrategy(QueryLookupStrategy.Key key, ValueExpressionDelegate valueExpressionDelegate) 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(BeanFactory beanFactory) voidsetEnvironment(Environment environment) voidsetEvaluationContextProvider(QueryMethodEvaluationContextProvider evaluationContextProvider) Deprecated.since 3.4, usesetEvaluationContextProvider(EvaluationContextProvider)instead.voidsetEvaluationContextProvider(EvaluationContextProvider evaluationContextProvider) Sets theEvaluationContextProviderto be used to evaluate SpEL expressions in manually defined queries.voidsetExposeMetadata(boolean exposeMetadata) Set whether the repository method metadata should be exposed by the repository factory as a ThreadLocal for retrieval via theRepositoryMethodContextclass.voidsetNamedQueries(NamedQueries namedQueries) Configures aNamedQueriesinstance to be handed to theQueryLookupStrategyfor query creation.voidSets the strategy of how to look up 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
-
setExposeMetadata
public void setExposeMetadata(boolean exposeMetadata) Set whether the repository method metadata should be exposed by the repository factory as a ThreadLocal for retrieval via theRepositoryMethodContextclass. This is useful if an advised object needs to obtain repository information.Default is "false", in order to avoid unnecessary extra interception. This means that no guarantees are provided that
RepositoryMethodContextaccess will work consistently within any method of the advised object.Repository method metadata is also exposed if implementations within the
repository compositionimplementRepositoryMetadataAccess.- Since:
- 3.4
- See Also:
-
setQueryLookupStrategyKey
Sets the strategy of how to look up 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 interfaceBeanClassLoaderAware
-
setBeanFactory
- Specified by:
setBeanFactoryin interfaceBeanFactoryAware- Throws:
BeansException
-
setEnvironment
- Specified by:
setEnvironmentin interfaceEnvironmentAware
-
getEnvironment
- Specified by:
getEnvironmentin interfaceEnvironmentCapable
-
setEvaluationContextProvider
@Deprecated(since="3.4") public void setEvaluationContextProvider(@Nullable QueryMethodEvaluationContextProvider evaluationContextProvider) Deprecated.since 3.4, usesetEvaluationContextProvider(EvaluationContextProvider)instead.Sets theQueryMethodEvaluationContextProviderto be used to evaluate SpEL expressions in manually defined queries.- Parameters:
evaluationContextProvider- can be null, defaults toQueryMethodEvaluationContextProvider.DEFAULT.
-
setEvaluationContextProvider
public void setEvaluationContextProvider(@Nullable EvaluationContextProvider evaluationContextProvider) Sets theEvaluationContextProviderto be used to evaluate SpEL expressions in manually defined queries.- Parameters:
evaluationContextProvider- can be null, defaults toEvaluationContextProvider.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- the listener to add.
-
addInvocationListener
Adds aRepositoryMethodInvocationListenerto the factory to plug in functionality triggered right after runningquery methodsandfragment methods.- Parameters:
listener- the listener to add.- 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- the post-processor to add.
-
getRepositoryFragments
protected RepositoryComposition.RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) CreatesRepositoryComposition.RepositoryFragmentsbased onRepositoryMetadatato add repository-specific extensions.- Parameters:
metadata- the repository metadata to use.- Returns:
- fragment composition.
-
getRepository
Returns a repository instance for the given interface.- Parameters:
repositoryInterface- must not be null.- Returns:
- the implemented repository interface.
-
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:
- the implemented repository interface.
-
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:
- the implemented repository interface.
- Since:
- 2.0
-
getProjectionFactory
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
@Deprecated(since="3.4") protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLookupStrategy.Key key, QueryMethodEvaluationContextProvider evaluationContextProvider) Deprecated.since 3.4, usegetQueryLookupStrategy(Key, ValueExpressionDelegate)instead to supportValueExpressionin query methods.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
-
getQueryLookupStrategy
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLookupStrategy.Key key, ValueExpressionDelegate valueExpressionDelegate) Returns theQueryLookupStrategyfor the givenQueryLookupStrategy.KeyandValueExpressionDelegate. Favor implementing this method overgetQueryLookupStrategy(Key, QueryMethodEvaluationContextProvider)for extendedValueExpressionsupport.This method delegates to
getQueryLookupStrategy(Key, QueryMethodEvaluationContextProvider)unless overridden.- Parameters:
key- can be null.valueExpressionDelegate- will never be null.- Returns:
- the
QueryLookupStrategyto use or null if no queries should be looked up. - Since:
- 3.4
-
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
-
getQueryLookupStrategy(Key, ValueExpressionDelegate)instead to supportValueExpressionin query methods.