DATAMONGO-1894 - Introduce cached ExpressionParser.

Original Pull Request: #874
This commit is contained in:
Mark Paluch
2020-06-30 14:38:07 +02:00
committed by Christoph Strobl
parent 66fae82798
commit 41607b10d0
19 changed files with 147 additions and 86 deletions

View File

@@ -5,6 +5,7 @@
== What's New in Spring Data MongoDB 3.1
* <<mongo.auditing,Reactive auditing>> enabled through `@EnableReactiveMongoAuditing`. `@EnableMongoAuditing` no longer registers `ReactiveAuditingEntityCallback`.
* Reactive SpEL support in `@Query` and `@Aggregation` query methods.
[[new-features.3.0]]
== What's New in Spring Data MongoDB 3.0

View File

@@ -131,7 +131,7 @@ The preceding example creates an application context with Spring's unit test sup
[[mongodb.repositories.queries]]
== Query Methods
Most of the data access operations you usually trigger on a repository result in a query being executed against the MongoDB databases. Defining such a query is a matter of declaring a method on the repository interface, as the following example shows:
Most of the data access operations you usually trigger on a repository result in a query being executed against the MongoDB databases.Defining such a query is a matter of declaring a method on the repository interface, as the following example shows:
.PersonRepository with query methods
====
@@ -445,7 +445,7 @@ results in `{ age : -1 }`. Using `Sort.by(ASC, "age")` overrides the defaults an
Query strings and field definitions can be used together with SpEL expressions to create dynamic queries at runtime.
SpEL expressions can provide predicate values and can be used to extend predicates with subdocuments.
Expressions expose method arguments through an array that contains all the arguments. The following query uses `[0]`
Expressions expose method arguments through an array that contains all the arguments.The following query uses `[0]`
to declare the predicate value for `lastname` (which is equivalent to the `?0` parameter binding):
[source,java]
@@ -457,7 +457,7 @@ public interface PersonRepository extends MongoRepository<Person, String> {
}
----
Expressions can be used to invoke functions, evaluate conditionals, and construct values. SpEL expressions
Expressions can be used to invoke functions, evaluate conditionals, and construct values.SpEL expressions
used in conjunction with JSON reveal a side-effect, because Map-like declarations inside of SpEL read like JSON, as the following example shows:
[source,java]
@@ -469,12 +469,12 @@ public interface PersonRepository extends MongoRepository<Person, String> {
}
----
SpEL in query strings can be a powerful way to enhance queries. However, they can also accept a broad range of unwanted arguments.
SpEL in query strings can be a powerful way to enhance queries.However, they can also accept a broad range of unwanted arguments.
You should make sure to sanitize strings before passing them to the query to avoid unwanted changes to your query.
Expression support is extensible through the Query SPI: `org.springframework.data.repository.query.spi.EvaluationContextExtension`.
The Query SPI can contribute properties and functions and can customize the root object. Extensions are retrieved from the application context
at the time of SpEL evaluation when the query is built. The following example shows how to use `EvaluationContextExtension`:
The Query SPI can contribute properties and functions and can customize the root object.Extensions are retrieved from the application context
at the time of SpEL evaluation when the query is built.The following example shows how to use `EvaluationContextExtension`:
[source,java]
----
@@ -492,13 +492,15 @@ public class SampleEvaluationContextExtension extends EvaluationContextExtension
}
----
NOTE: Bootstrapping `MongoRepositoryFactory` yourself is not application context-aware and requires further configuration
to pick up Query SPI extensions.
NOTE: Bootstrapping `MongoRepositoryFactory` yourself is not application context-aware and requires further configuration to pick up Query SPI extensions.
NOTE: Reactive query methods can make use of `org.springframework.data.spel.spi.ReactiveEvaluationContextExtension`.
[[mongodb.repositories.queries.type-safe]]
=== Type-safe Query Methods
MongoDB repository support integrates with the http://www.querydsl.com/[Querydsl] project, which provides a way to perform type-safe queries. To quote from the project description, "Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API." It provides the following features:
MongoDB repository support integrates with the http://www.querydsl.com/[Querydsl] project, which provides a way to perform type-safe queries.
To quote from the project description, "Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API." It provides the following features:
* Code completion in the IDE (all properties, methods, and operations can be expanded in your favorite Java IDE).
* Almost no syntactically invalid queries allowed (type-safe on all levels).
@@ -519,7 +521,7 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"),
PageRequest.of(0, 2, Direction.ASC, "lastname"));
----
`QPerson` is a class that is generated by the Java annotation post-processing tool. It is a `Predicate` that lets you write type-safe queries. Notice that there are no strings in the query other than the `C0123` value.
`QPerson` is a class that is generated by the Java annotation post-processing tool.It is a `Predicate` that lets you write type-safe queries.Notice that there are no strings in the query other than the `C0123` value.
You can use the generated `Predicate` class by using the `QuerydslPredicateExecutor` interface, which the following listing shows: