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 TypeMethodDescriptionvoid
Adds aRepositoryMethodInvocationListener
to the factory to plug in functionality triggered right after runningquery methods
andfragment methods
.void
addQueryCreationListener
(QueryCreationListener<?> listener) Adds aQueryCreationListener
to the factory to plug in functionality triggered right after creation ofRepositoryQuery
instances.void
AddsRepositoryProxyPostProcessor
s to the factory to allow manipulation of theProxyFactory
before the proxy gets created.abstract <T,
ID> EntityInformation<T, ID> getEntityInformation
(Class<T> domainClass) Returns theEntityInformation
for the given domain class.protected ProjectionFactory
Returns aProjectionFactory
instance.protected ProjectionFactory
getProjectionFactory
(ClassLoader classLoader, BeanFactory beanFactory) Returns theProjectionFactory
to 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> T
getRepository
(Class<T> repositoryInterface) Returns a repository instance for the given interface.<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.<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.protected abstract Class<?>
getRepositoryBaseClass
(RepositoryMetadata metadata) Returns the base class backing the actual repository instance.getRepositoryFragments
(RepositoryMetadata metadata) CreatesRepositoryComposition.RepositoryFragments
based onRepositoryMetadata
to add repository-specific extensions.protected RepositoryInformation
getRepositoryInformation
(RepositoryMetadata metadata, RepositoryComposition.RepositoryFragments fragments) Returns theRepositoryInformation
for the givenRepositoryMetadata
and customRepositoryComposition.RepositoryFragments
.protected RepositoryMetadata
getRepositoryMetadata
(Class<?> repositoryInterface) Returns theRepositoryMetadata
for the given repository interface.protected abstract Object
getTargetRepository
(RepositoryInformation metadata) Create a repository instance as backing for the query proxy.protected final <R> R
getTargetRepositoryViaReflection
(Class<?> baseClass, Object... constructorArguments) Deprecated.since 2.6 because it has a misleading name.protected final <R> R
getTargetRepositoryViaReflection
(RepositoryInformation information, Object... constructorArguments) Creates a repository of the repository base class defined in the givenRepositoryInformation
using reflection.protected final <R> R
instantiateClass
(Class<?> baseClass, Object... constructorArguments) Convenience method to instantiate a class using the givenconstructorArguments
by looking up a matching constructor.void
setBeanClassLoader
(ClassLoader classLoader) void
setBeanFactory
(BeanFactory beanFactory) void
setEnvironment
(Environment environment) void
setEvaluationContextProvider
(QueryMethodEvaluationContextProvider evaluationContextProvider) Deprecated.since 3.4, usesetEvaluationContextProvider(EvaluationContextProvider)
instead.void
setEvaluationContextProvider
(EvaluationContextProvider evaluationContextProvider) Sets theEvaluationContextProvider
to be used to evaluate SpEL expressions in manually defined queries.void
setExposeMetadata
(boolean exposeMetadata) Set whether the repository method metadata should be exposed by the repository factory as a ThreadLocal for retrieval via theRepositoryMethodContext
class.void
setNamedQueries
(NamedQueries namedQueries) Configures aNamedQueries
instance to be handed to theQueryLookupStrategy
for query creation.void
Sets the strategy of how to look up a query to execute finders.void
setRepositoryBaseClass
(Class<?> repositoryBaseClass) Configures the repository base class to use when creating the repository proxy.protected void
validate
(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 theRepositoryMethodContext
class. 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
RepositoryMethodContext
access will work consistently within any method of the advised object.Repository method metadata is also exposed if implementations within the
repository composition
implementRepositoryMetadataAccess
.- Since:
- 3.4
- See Also:
-
setQueryLookupStrategyKey
Sets the strategy of how to look up a query to execute finders.- Parameters:
key
-
-
setNamedQueries
Configures aNamedQueries
instance to be handed to theQueryLookupStrategy
for query creation.- Parameters:
namedQueries
- the namedQueries to set
-
setBeanClassLoader
- Specified by:
setBeanClassLoader
in interfaceBeanClassLoaderAware
-
setBeanFactory
- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Throws:
BeansException
-
setEnvironment
- Specified by:
setEnvironment
in interfaceEnvironmentAware
-
getEnvironment
- Specified by:
getEnvironment
in interfaceEnvironmentCapable
-
setEvaluationContextProvider
@Deprecated(since="3.4") public void setEvaluationContextProvider(@Nullable QueryMethodEvaluationContextProvider evaluationContextProvider) Deprecated.since 3.4, usesetEvaluationContextProvider(EvaluationContextProvider)
instead.Sets theQueryMethodEvaluationContextProvider
to 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 theEvaluationContextProvider
to 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 aQueryCreationListener
to the factory to plug in functionality triggered right after creation ofRepositoryQuery
instances.- Parameters:
listener
- the listener to add.
-
addInvocationListener
Adds aRepositoryMethodInvocationListener
to the factory to plug in functionality triggered right after runningquery methods
andfragment methods
.- Parameters:
listener
- the listener to add.- Since:
- 2.4
-
addRepositoryProxyPostProcessor
AddsRepositoryProxyPostProcessor
s to the factory to allow manipulation of theProxyFactory
before the proxy gets created. Note that theQueryExecutorMethodInterceptor
will be added to the proxy after theRepositoryProxyPostProcessor
s are considered.- Parameters:
processor
- the post-processor to add.
-
getRepositoryFragments
protected RepositoryComposition.RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) CreatesRepositoryComposition.RepositoryFragments
based onRepositoryMetadata
to 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 theProjectionFactory
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
Returns theRepositoryMetadata
for the given repository interface.- Parameters:
repositoryInterface
- will never be null.- Returns:
-
getRepositoryInformation
protected RepositoryInformation getRepositoryInformation(RepositoryMetadata metadata, RepositoryComposition.RepositoryFragments fragments) Returns theRepositoryInformation
for the givenRepositoryMetadata
and customRepositoryComposition.RepositoryFragments
.- Parameters:
metadata
- must not be null.fragments
- must not be null.- Returns:
- will never be null.
-
getQueryMethods
-
getProjectionFactory
Returns aProjectionFactory
instance.- Returns:
- Since:
- 2.6
-
getEntityInformation
Returns theEntityInformation
for 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 supportValueExpression
in query methods.Returns theQueryLookupStrategy
for the givenQueryLookupStrategy.Key
andQueryMethodEvaluationContextProvider
.- 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
-
getQueryLookupStrategy
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLookupStrategy.Key key, ValueExpressionDelegate valueExpressionDelegate) Returns theQueryLookupStrategy
for the givenQueryLookupStrategy.Key
andValueExpressionDelegate
. Favor implementing this method overgetQueryLookupStrategy(Key, QueryMethodEvaluationContextProvider)
for extendedValueExpression
support.This method delegates to
getQueryLookupStrategy(Key, QueryMethodEvaluationContextProvider)
unless overridden.- Parameters:
key
- can be null.valueExpressionDelegate
- will never be null.- Returns:
- the
QueryLookupStrategy
to 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 givenRepositoryInformation
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. UseinstantiateClass(Class, Object...)
instead.Creates a repository of the repository base class defined in the givenRepositoryInformation
using reflection.- Parameters:
baseClass
-constructorArguments
-- Returns:
-
instantiateClass
Convenience method to instantiate a class using the givenconstructorArguments
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
-
getQueryLookupStrategy(Key, ValueExpressionDelegate)
instead to supportValueExpression
in query methods.