且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查看 Spring Data MongoDB 生成的存储库实现?

更新时间:2023-02-13 15:00:40

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:

  1. CrudRepository 的 store 特定实现.查看名为 Simple(Jpa|Mongo|Neo4|…)Repository 的类型(参见 JPA 特定的 这里).他们有真实的"CrudRepositoryPagingAndSortingRepository 中所有方法的实现.

  1. The store specific implementation of CrudRepository. Have a look for types named Simple(Jpa|Mongo|Neo4|…)Repository (see the JPA specific one here). They have "real" implementations for all of the methods in CrudRepository and PagingAndSortingRepository.

查询方法由 QueryExecutorMethodInterceptor.doInvoke(…) 有效执行(参见 这里).找到委托目标并调用它基本上是一个 3 步过程.实际的执行是在名为 (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).

直接调用自定义实现代码,同样来自QueryExecutorMethodInterceptor.

Custom implementation code is called directly, also from QueryExecutorMethodInterceptor.

唯一剩下的就是查询派生,它由两个主要部分组成:方法名解析和查询创建.对于前者,请查看 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).