17 Spring Data JPA analysis of common interface

Think

In the case of customers, we found that custom CustomerDao, and did not provide any method you can use many of these methods, these methods exactly how to do? The answer is simple, the interface for our custom Dao, an inheritance from JpaRepository and JpaSpecificationExecutor, so we can use all the methods in both interfaces.

When using Spring Data JPA, the general realization JpaRepository and JpaSpecificationExecutor interfaces, so that these methods defined in the interface can be used, but these are just some of the statements, no specific implementation, then it is how to achieve the Spring Data JPA in it?

Process Analysis

When the program executed, will invoke the method by JdkDynamicAopProxy generate dynamic proxy objects customerDao objects. According to Spring Data JPA introduced with knowledge, in order to carry out findOne query methods, eventually JPA API specification will be done, then the underlying code of where it exists? The answer is simple, hidden in a dynamic proxy object generated by JdkDynamicAopProxy them, and this dynamic proxy object is SimpleJpaRepository

1 We write only interface, but did not write the implementation class, the implementation class is Spring proxy object at run time, injected.
2 .Spring know how to generate that dao implementation class? Because the interface is specified package dao resides in the configuration file

  <jpa:repositories base-package="cn.oracle.dao"
  transaction-manager-ref="transactionManager"
  entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>

3 . What is generated objects? Proxy object: jdk generated by the Proxy.newProxyInstance dynamic proxy object (class loader, a list of interfaces implemented, InvocationHandler) JdkDynamicAopProxy: This object is achieved InvocationHandler interface, this class has the invoke method


4 in the invoke method JdkDynamicAopProxy. there is a target object, which is the real work of the object

5 real work objects:. SimpleJpaRepository

6 .SimpleJpaRepository dao interface inheritance we realized that the two interfaces, it certainly has all the interface methods in this class

7. we see findOne method called em.find method, the em in the end is who?

 

Guess you like

Origin www.cnblogs.com/zhaochengf/p/12127772.html