问题描述
Spring Data何时生成存储库的实现?在编译时还是在运行时?我可以看到Spring Data生成的实现存储库实现吗?
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?
推荐答案
tl; dr
不,原因很简单:没有代码生成正在进行.该实现基于代理和方法拦截器,将调用执行委派到正确的位置.
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.
有效地,方法执行可以由3种类型的代码支持:
Effectively, a method execution can be backed by 3 types of code:
-
CrudRepository
的商店特定实现.看一下名为Simple(Jpa|Mongo|Neo4|…)Repository
的类型(请参阅JPA特定的一种).对于CrudRepository
和PagingAndSortingRepository
中的所有方法,它们都有真实的"实现.
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 inCrudRepository
andPagingAndSortingRepository
.
查询方法由QueryExecutorMethodInterceptor.doInvoke(…)
有效执行(请参见).查找授权目标并调用它基本上是一个三步过程.实际执行是在名为(Jpa|Mongo|Neo4j…)QueryExecution
的类中完成的(例如,请参见此示例).
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).
剩下的唯一一件事就是查询派生,它由两个主要部分组成:方法名称解析和查询创建.对于前者,请查看PartTree
.它带有一个方法名称和一个基本类型,并且将返回一个已解析的类似AST的结构,如果无法解析属性等,则将引发异常.
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.
后者在名为PartTree(Jpa|Mongo|Neo4j|…)Query
的类中实现,并委托给其他组件以实际创建商店特定的查询.例如.对于JPA,有趣的地方可能在JpaQueryCreator.PredicateBuilder.build()
中(请参阅).
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).
这篇关于如何查看Spring Data MongoDB生成的存储库实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!