2. Initialization depth analysis of MyBatis Mapper Bean 5-3

Keywords: Programming Mybatis Spring Attribute

III. load MapperScannerRegistrar

The startup class code is as follows:

package com.iwill.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@MapperScan(value = {"com.iwill.mybatis.dao.mapper.ext", "com.iwill.mybatis.dao.mapper.gen"})
@EnableTransactionManagement(proxyTargetClass = true)
public class MyBatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyBatisApplication.class, args);

When loading the configuration annotation of MyBatisApplication, the annotation on MyBatisApplication will be resolved as follows:

It will parse the @Import , and put the corresponding value attribute value into imports, which will be returned.

The MapperScan interface is defined as follows:


As you can see, the value of @ Import is MapperScannerRegistrar.class. As you can see, the annotation @ MapperScan uses MapperScannerRegistrar to load mapper bean s.

The source code of getImports is as follows:

	private Set<SourceClass> getImports(SourceClass sourceClass) throws IOException {
		Set<SourceClass> imports = new LinkedHashSet<>();
		Set<SourceClass> visited = new LinkedHashSet<>();
		collectImports(sourceClass, imports, visited);
		return imports;

Back to the previous step, getImports will be called by processImports method:

In processImports, MapperScannerRegistrar is placed in the importBeanDefinitionRegistrars list:

Posted by suzzane2020 on Sun, 01 Dec 2019 04:16:48 -0800