Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Principle of Mybatis-spring automatic injection Mechanism

Shulou Source: shulou.com Published: 2022-06-02 05:05:30 10月11日 Update

This article mainly introduces "the principle of Mybatis-spring automatic injection mechanism". In daily operation, I believe many people have doubts about the principle of Mybatis-spring automatic injection mechanism. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "Mybatis-spring automatic injection mechanism principle". Next, please follow the editor to study!

I. basic background

There are two main purposes of this article:

Mybatis is used in the project, and we only define the interface of mapper, but not implement it, so how is the bean of these implementation classes generated?

How is the instance bean of the mapper interface injected into the spring container?

Package name version number spring-boot2.1.9.RELEASEmybatis-spring2.0.0mybatis-plus-boot-starter3.1.0mybatis3.5.0 II, source code analysis

In the project, we use @ MapperScan to configure the path of the mapper interface, as shown in the following figure

EnableTransactionManagement@Configuration@MapperScan ("com.ke.newhouse.agency.project.dao.mapper") public class MybatisPlusConfiguration {@ Bean public PaginationInterceptor paginationInterceptor () {return new PaginationInterceptor ();}}

Under these mapper paths are the interfaces we defined. What are the functions of @ MapperScan? let's first take a look at the definition of @ MapperScan.

@ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.TYPE) @ Documented@Import (MapperScannerRegistrar.class) @ Repeatable (MapperScans.class) public @ interface MapperScan {}

In the definition of MapperScan, we only need to focus on @ Import (MapperScannerRegistrar.class), which means to introduce MapperScannerRegistrar.class into the spring container.

Public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {private ResourceLoader resourceLoader; / * * {@ inheritDoc} * / @ Override public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {AnnotationAttributes mapperScanAttrs = AnnotationAttributes .fromMap (importingClassMetadata.getAnnotationAttributes (MapperScan.class.getName (); if (mapperScanAttrs! = null) {registerBeanDefinitions (mapperScanAttrs, registry);}} void registerBeanDefinitions (AnnotationAttributes annoAttrs, BeanDefinitionRegistry registry) {ClassPathMapperScanner scanner = new ClassPathMapperScanner (registry) / / this check is needed in Spring 3.1 Optional.ofNullable (resourceLoader) .ifPresent (scanner::setResourceLoader); Class markerInterface = annoAttrs.getClass ("markerInterface"); if (! Class.class.equals (markerInterface)) {scanner.setMarkerInterface (markerInterface);} Class

Tags: Interfaces methods factories agents principles mechanisms instances containers learning generation analysis functions at the same time fundamentals properties articles methods times more purposes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS MariaDB Huawei OPPO Reno