[Mybatis source code analysis] Mybatis dao proxy object injection and use of the underlying process

doubt

When we use mybatis, we need to inject dao into the service layer, the following code:


Then I obviously did not implement the dao interface, how does the framework implement the injected implementation class?
After debugging a bit, it turns out that the MapperProxy class was injected. Obviously, a dynamic proxy was used.

Note: This chapter requires readers to be very proficient in spring bean life cycle and dynamic proxy knowledge

reference:

Mainly divided into 3 major steps:

  1. @MapperScan annotation scanning dao process

  2. @Autowired annotation process of injecting dao

  3. The use process of dao

 

One, @MapperScan annotation scanning dao process

1. When the springboot project starts, the annotations will be scanned when the spring container is initialized, and the @MapperScan annotation is found.

2. Because the @MapperScan annotation contains the @Import annotation, it starts to execute the registration MapperScannerRegistrar class


3. The ImportBeanDefinitionRegistrar class implements the ImportBeanDefinitionRegistrar interface, so the registerBeanDefinitions method will be executed to manually register the BeanDefinition.


4. Only one MapperScannerConfigurer class is registered in the above code, and because this class implements the BeanDefinitionRegistryPostProcessor interface, it starts to execute the postProcessBeanDefinitionRegistry method again

5. This class mainly creates a ClassPathMapperScanner class, which is used to scan the package of @MapperScan annotation configuration (scan the Dao interface in the project and register it in the spring container)

6. The doScan method of the ClassPathMapperScanner class is divided into two parts.

The first part is the registered beanDefinition of the normal spring. For example: beanName is UserDao, beanClass is com.example.demo.UserDao

The second part is mybatis modify beanDefinition: for example: beanName is UserDao, beanClass is changed to org.apache.ibatis.binding.MapperFactoryBean.

In order to allow Spring to create an object of type MapperFactoryBean when creating a bean, it is convenient to inject the UserDao object through the @Autowired annotation in the next step.
 

 

Second, the process of injecting dao through @Autowired

Now comes the spring attribute injection stage, for example: the service class needs to be injected into dao. Spring will produce a bean named UserDao according to the dao beanDefinition and the type is MapperProxy.

1. In the production process, since MapperFactoryBean implements the FactoryBean interface (spring's extended interface), this type of getObject() method is called to return the instance object.

(Normal objects are created by spring reflection, and beans that implement the FactoryBean interface are created by users themselves)

Reference for FactoryBean interface: https://blog.csdn.net/sumengnan/article/details/113634063

2. This method finally gets the MapperProxyFactory factory through the getMapper method of the mapperRegistry object

3. Through the MapperProxyFactory factory, create jdk dynamic proxy objects, using MapperProxy as a processor

4. Because the MapperProxy class implements the InvocationHandler interface, there is an invoke method that can be used as a processor.

 

Third, the use process of dao

1. When we call UserDao, we actually call the invoke method of MapperProxy.

2. The cachedInvoker method is generally used. First query the MapperMethodInvoker from the cache, if it does not exist, create a class of PlainMethodInvoker (the constructor is the MapperMethod class)

3. The class of PlainMethodInvoker will be put into the methodCache cache, and the invoke method will be executed

 

3. Continue to call the execute method of the MapperMethod class. (This class is really used to perform database operations)

4. For example, my execution sql is a select statement. In the execute method, it is judged as SELECT, and finally the database operation performed by the sqlSession class.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/113953507
Recommended