First of all, there's no code generation going on, which means: no CGLib, no byte-code generation at all. The fundamental approach is that a JDK proxy instance is created programmatically using Spring's ProxyFactory
API to back the interface and a MethodInterceptor
intercepts all calls to the instance and routes the method into the appropriate places:
- If the repository has been initialized with a custom implementation part (see that part of the reference documentation for details), and the method invoked is implemented in that class, the call is routed there.
- If the method is a query method (see
DefaultRepositoryInformation
for how that is determined), the store specific query execution mechanism kicks in and executes the query determined to be executed for that method at startup. For that a resolution mechanism is in place that tries to identify explicitly declared queries in various places (using @Query
on the method, JPA named queries) eventually falling back to query derivation from the method name. For the query mechanism detection, see JpaQueryLookupStrategy
. The parsing logic for the query derivation can be found in PartTree
. The store specific translation into an actual query can be seen e.g. in JpaQueryCreator
.
- If none of the above apply the method executed has to be one implemented by a store-specific repository base class (
SimpleJpaRepository
in case of JPA) and the call is routed into an instance of that.
The method interceptor implementing that routing logic is QueryExecutorMethodInterceptor
, the high level routing logic can be found here.
The creation of those proxies is encapsulated into a standard Java based Factory pattern implementation. The high-level proxy creation can be found in RepositoryFactorySupport
. The store-specific implementations then add the necessary infrastructure components so that for JPA you can go ahead and just write code like this:
EntityManager em = … // obtain an EntityManager
JpaRepositoryFactory factory = new JpaRepositoryFactory(em);
UserRepository repository = factory.getRepository(UserRepository.class);
The reason I mention that explicitly is that it should become clear that, in its core, nothing of that code requires a Spring container to run in the first place. It needs Spring as a library on the classpath (because we prefer to not reinvent the wheel), but is container agnostic in general.
To ease the integration with DI containers we've of course then built integration with Spring Java configuration, an XML namespace, but also a CDI extension, so that Spring Data can be used in plain CDI scenarios.
@Repository
annotated interfaces in the first place? Looking atRepositoryFactorySupport#getRepository()
show that it takes the interface class as a parameter, so it must be discovered somewhere else. I'm particularly trying to figure out how to find an annotated interface and automatically generate a JDK proxy bean which implements the interface, very much like spring-data, but for an application-specific purpose not related to Repositories. – Coltson