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.<T,ID> EntityInformation <T, ID> getEntityInformation(Class<T> domainClass) Deprecated.EntityInformation<?, ?> getEntityInformation(RepositoryMetadata metadata) Returns theEntityInformationfor the givenRepositoryMetadata.protected ProjectionFactoryReturns aProjectionFactoryinstance.protected ProjectionFactorygetProjectionFactory(@Nullable ClassLoader classLoader, @Nullable BeanFactory beanFactory) Returns theProjectionFactoryto be used with the repository instances created.protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable 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 instance of the repository base class providing store-specific built-in repository functionality of a typicalCrudRepository.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(@Nullable ClassLoader classLoader) voidsetBeanFactory(BeanFactory beanFactory) voidsetEnvironment(Environment environment) voidsetEvaluationContextProvider(@Nullable 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(@Nullable NamedQueries namedQueries) Configures aNamedQueriesinstance to be handed to theQueryLookupStrategyfor query creation.voidsetQueryLookupStrategyKey(@Nullable QueryLookupStrategy.Key key) Sets the strategy of how to look up a query to execute finders.voidsetRepositoryBaseClass(@Nullable Class<?> repositoryBaseClass) Configures the repository base class to use when creating the repository.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
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. If not set, the factory will use the type returned bygetRepositoryBaseClass(RepositoryMetadata)by default.- Parameters:
repositoryBaseClass- the repository base class to back the repository, 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
protected ProjectionFactory getProjectionFactory(@Nullable ClassLoader classLoader, @Nullable 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
@Deprecated(since="4.0") public <T,ID> EntityInformation<T,ID> getEntityInformation(Class<T> domainClass) Deprecated.since 4.0, usegetEntityInformation(RepositoryMetadata)instead.Returns theEntityInformationfor the given domain class.- Type Parameters:
T- the entity typeID- the id type- Parameters:
domainClass-- Returns:
-
getEntityInformation
Returns theEntityInformationfor the givenRepositoryMetadata.- Parameters:
metadata- must not be null.- Returns:
- the
EntityInformationto be used forRepositoryMetadata. - Since:
- 4.0
-
getTargetRepository
Create a instance of the repository base class providing store-specific built-in repository functionality of a typicalCrudRepository.- Parameters:
metadata- repository metadata.- Returns:
- object implementing the repository base functionality.
-
getRepositoryBaseClass
Returns the base class backing the actual repository instance. Make suregetTargetRepository(RepositoryInformation)returns an instance of this class.- Parameters:
metadata- repository metadata.- Returns:
- the repository base class.
-
getQueryLookupStrategy
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLookupStrategy.Key key, ValueExpressionDelegate valueExpressionDelegate) - 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. -
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.
- Since:
- 2.6
-
getEntityInformation(RepositoryMetadata)instead.