When is the implementation for repositories generated by Spring Data? At compile time or runtime? Can I see the implementation repository implementation generated by Spring Data?
- 80,157
- 18
- 225
- 211
- 798
- 1
- 13
- 30
-
1I am afraid, both these comments are totally unrelated to the question. – Oliver Drotbohm May 10 '14 at 08:32
1 Answers
tl;dr
No, for a very simple reason: there's no code generation going on. The implementation is based on proxies and a method interceptor delegating the call executions to the right places.
Details
Effectively, a method execution can be backed by 3 types of code:
The store specific implementation of
CrudRepository. Have a look for types namedSimple(Jpa|Mongo|Neo4|…)Repository(see the JPA specific one here). They have "real" implementations for all of the methods inCrudRepositoryandPagingAndSortingRepository.Query methods are effectively executed by
QueryExecutorMethodInterceptor.doInvoke(…)(see here). It's basically a 3-step-process to find the delegation target and invoke it. The actual execution is done in classes named(Jpa|Mongo|Neo4j…)QueryExecution(see this one for example).Custom implementation code is called directly, also from
QueryExecutorMethodInterceptor.
The only thing left is the query derivation, which consists of two major parts: method name parsing and query creation. For the former, have a look at PartTree. It takes a method name and a base type and will return you a parsed AST-like structure or throw an exception if it fails to resolve properties or the like.
The latter is implemented in classes named PartTree(Jpa|Mongo|Neo4j|…)Query and delegates to additional components for actually creating the store specific query. E.g. for JPA the interesting bits are probably in JpaQueryCreator.PredicateBuilder.build() (see here).
- 1
- 1
- 80,157
- 18
- 225
- 211
-
Hi Oliver, could you please explain a bit more on how QueryExecutorMethodInterceptor works and when does delegation and invocation happens? – xabhi Jul 07 '14 at 07:42
-
@xabhi — You might wanna have a look at this answer. https://stackoverflow.com/questions/38509882/how-are-spring-data-repositories-actually-implemented/38511337#38511337 – Oliver Drotbohm Nov 16 '16 at 22:41