1. Introduction

Sometimes you need to define your own annotations to mark up specific functionality classes and inject them into the Spring IoC container. The representative one is the Mapper interface of Mybatis. What do you do if a new requirement asks you to do something similar? Today we will start from the relevant functions of Mybatis to learn its ideas and for my use.

2. Mybatis Mapper registration mechanism

Mybatis will register Mapper with Spring IoC as follows:

In fact, there are many knowledge points related to Spring and Mybatis, but as long as we sort out the process, it will be easier to understand and master. So the essence of reading source code is to grasp the context of a leaf, and then each break to comb its direction. So Chubby combs out the left is the right “vein”, and we’ll dissect them step by step.

3. ImportBeanDefinitionRegistrar

ImportBeanDefinitionRegistrar is a very important interface, all of the third party integrated into Spring developers should grasp this interface. This interface is used to dynamically register a set of classes with the same characteristics to Spring IoC, similar to the ImportSelector interface, which “attaches” to custom annotations with the @import annotation, just as Mybatis-Spring uses.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
@Repeatable(MapperScans.class)
public @interface MapperScan {
    / / to omit
}
Copy the code

You can also attach it directly to a Configuration class marked @Configuration or with the same functionality.

@Import(MapperScannerRegistrar.class)
@Configuration
public class MyConfig {}Copy the code

It has only one method:

void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
Copy the code

The importingClassMetadata parameter contains all annotations on the configuration class to which @import is attached. This means that we can get the meta information for the annotation and use it as a basis for our dynamic import, which is the package and other information for the Mapper from @mapperscan. The BeanDefinitionRegistry is used to register Spring beans. So how do you register? Which brings us to our next hero.

4. BeanDefinitionRegistryPostProcessor

BeanDefinitionRegistryPostProcessor Spring BeanFactoryPostProcessor as part of the interface, the Spring BeanFactoryPostProcessor is used in the Spring Execute postProcessBeanFactory() to handle some additional logic when the Bean definition information is loaded but not initialized, And BeanDefinitionRegistryPostProcessor role is in the spring BeanFactoryPostProcessor added a preposition, when a Bean implements this interface, The implementation of the interface before beginning the postProcessBeanDefinitionRegistry () method, and then execute the method of its parent class postProcessBeanFactory (). This refines the initialization cycle of a Spring Bean, giving us the possibility to customize it at various stages.

For the purposes of this article, however, this class can be ignored. It only triggers the batch scan injection logic, and it does not actually participate in scan injection.

5. ClassPathBeanDefinitionScanner

By definition, this class scans Bean definitions under the classpath and registers qualified batches with Spring IoC via the BeanDefinitionRegistry. It provides some default filters to check out beans that need to be injected into Spring IoC, using JSR 250 and JSR 330 annotations by default. Of course, you can add included beans by addIncludeFilter, or exclude some beans by addExcludeFilter. Then you just need to call its SCAN method to scan injection for that particular package.

6. FactoryBean

Just like Mapper of Mybatis, they share common features while also having some differences. So using the FactoryBean interface to create these MapPers makes perfect sense. About FactoryBeans, I have devoted my article factoryBeans and BeanFactory in Spring to explain them. If you are interested, go there.

But FactoryBean is not a mandatory step for dynamic scan injection.

7. To summarize

This article through to Mybatis injection mechanism are analyzed to study the ImportBeanDefinitionRegistrar lifecycle and use. How to use it to write our own injection logic is the most important, I will talk about some practical applications in this area, please stay tuned: code farmer xiao Pang brother.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn