i have been working spring data jpa repository in project time , know below points:
- in repository interfaces, can add methods
findbycustomernameandphone()
(assumingcustomername
,phone
fields in domain object). - then, spring provides implementation implementing above repository interface methods @ runtime (during application run).
i interested on how has been coded , have looked @ spring jpa source code & apis, not find answers questions below:
- how repository implementation class generated @ runtime & methods being implemented , injected?
- does spring data jpa use cglib or bytecode manipulation libraries implement methods , inject dynamically?
could please above queries , provide supported documentation ?
first of all, there's no code generation going on, means: no cglib, no byte-code generation @ all. fundamental approach jdk proxy instance created programmatically using spring's proxyfactory
api interface , methodinterceptor
intercepts calls instance , routes method appropriate places:
- if repository has been initialized custom implementation part (see that part of reference documentation details), , method invoked implemented in class, call routed there.
- if method query method (see
defaultrepositoryinformation
how determined), store specific query execution mechanism kicks in , executes query determined executed method @ startup. resolution mechanism in place tries identify explicitly declared queries in various places (using@query
on method, jpa named queries) falling query derivation method name. query mechanism detection, seejpaquerylookupstrategy
. parsing logic query derivation can found inparttree
. store specific translation actual query can seen e.g. injpaquerycreator
. - if none of above apply method executed has 1 implemented store-specific repository base class (
simplejparepository
in case of jpa) , call routed instance of that.
the method interceptor implementing routing logic queryexecutormethodinterceptor
, high level routing logic can found here.
the creation of proxies encapsulated standard java based factory pattern implementation. high-level proxy creation can found in repositoryfactorysupport
. store-specific implementations add necessary infrastructure components jpa can go ahead , write code this:
entitymanager em = … // obtain entitymanager jparepositoryfactory factory = new jparepositoryfactory(em); userrepository repository = factory.getrepository(userrepository.class);
the reason mention explicitly should become clear that, in core, nothing of code requires spring container run in first place. needs spring library on classpath (because prefer not reinvent wheel), container agnostic in general.
to ease integration di containers we've of course built integration spring java configuration, xml namespace, cdi extension, spring data can used in plain cdi scenarios.
Comments
Post a Comment