Class SpelQueryContext
java.lang.Object
org.springframework.data.repository.query.SpelQueryContext
- Direct Known Subclasses:
SpelQueryContext.EvaluatingSpelQueryContext
A SpelQueryContext is able to find SpEL expressions in a query string and to replace them with bind variables.
Result o the parse process is a SpelQueryContext.SpelExtractor
which offers the transformed query string.
Alternatively and preferred one may provide a QueryMethodEvaluationContextProvider
via
withEvaluationContextProvider(QueryMethodEvaluationContextProvider)
which will yield the more powerful
SpelQueryContext.EvaluatingSpelQueryContext
.
Typical usage looks like
SpelQueryContext.EvaluatingSpelQueryContext queryContext = SpelQueryContext
.of((counter, expression) -> String.format("__$synthetic$__%d", counter), String::concat)
.withEvaluationContextProvider(evaluationContextProvider);
SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
- Since:
- 2.1
- Author:
- Jens Schauder, Gerrit Meier, Mark Paluch
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
An extension ofSpelQueryContext
that can createSpelEvaluator
instances as it also knows about aQueryMethodEvaluationContextProvider
.class
Parses a query string, identifies the contained SpEL expressions, replaces them with bind parameters and offers aMap
from those bind parameters to the SpEL expression. -
Method Summary
Modifier and TypeMethodDescriptionstatic SpelQueryContext
of
(BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) Parses the query for SpEL expressions using the pattern:CreatesaSpelQueryContext.EvaluatingSpelQueryContext
from the current one and the givenQueryMethodEvaluationContextProvider
.
-
Method Details
-
of
public static SpelQueryContext of(BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) -
parse
Parses the query for SpEL expressions using the pattern:<prefix>#{<spel>}
with prefix being the character ':' or '?'. Parsing honors quoted Strings enclosed in single or double quotation marks.
- Parameters:
query
- a query containing SpEL expressions in the format described above. Must not be null.- Returns:
- A
SpelQueryContext.SpelExtractor
which makes the query with SpEL expressions replaced by bind parameters and a map from bind parameter to SpEL expression available. Guaranteed to be not null.
-
withEvaluationContextProvider
public SpelQueryContext.EvaluatingSpelQueryContext withEvaluationContextProvider(QueryMethodEvaluationContextProvider provider) CreatesaSpelQueryContext.EvaluatingSpelQueryContext
from the current one and the givenQueryMethodEvaluationContextProvider
.- Parameters:
provider
- must not be null.- Returns:
-