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; @SpringBootApplication @ComponentScan("com.iwill.mybatis") @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:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Import(MapperScannerRegistrar.class) @Repeatable(MapperScans.class{ ...... }
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: