大家好,又见面了,我是你们的朋友全栈君。
目录
0.说明
1.@MapperScan
2. MapperScannerRegister
3.ClassPathMapperScanner
4. MapperFactoryBean
0.说明
mybatis构建过程主要包括:
- 解析mybatis配置文件,构造Configuration配置类对象和SqlSessionFactory;
- 利用@MapperScan注册BeanDefinition到BeanFactory工厂中;
其中第一步中创建了Mapper接口代理类,并存储到Configuration中;
下面主要介绍第二步,利用@MapperScan注册BeanDefinition的过程;
1.@MapperScan
代码语言:javascript复制/**
* Use this annotation to register MyBatis mapper interfaces when using Java
* Config. It performs when same work as {@link MapperScannerConfigurer} via
* {@link MapperScannerRegistrar}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {
/**
* Alias for the {@link #basePackages()} attribute. Allows for more concise
* annotation declarations e.g.:
* {@code @EnableMyBatisMapperScanner("org.my.pkg")} instead of {@code
* @EnableMyBatisMapperScanner(basePackages= "org.my.pkg"})}.
*/
String[] value() default {};
/**
* Base packages to scan for MyBatis interfaces. Note that only interfaces
* with at least one method will be registered; concrete classes will be
* ignored.
*/
String[] basePackages() default {};
/**
* Type-safe alternative to {@link #basePackages()} for specifying the packages
* to scan for annotated components. The package of each class specified will be scanned.
* <p>Consider creating a special no-op marker class or interface in each package
* that serves no purpose other than being referenced by this attribute.
*/
Class<?>[] basePackageClasses() default {};
/**
* The {@link BeanNameGenerator} class to be used for naming detected components
* within the Spring container.
*/
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
/**
* This property specifies the annotation that the scanner will search for.
* <p>
* The scanner will register all interfaces in the base package that also have
* the specified annotation.
* <p>
* Note this can be combined with markerInterface.
*/
Class<? extends Annotation> annotationClass() default Annotation.class;
/**
* This property specifies the parent that the scanner will search for.
* <p>
* The scanner will register all interfaces in the base package that also have
* the specified interface class as a parent.
* <p>
* Note this can be combined with annotationClass.
*/
Class<?> markerInterface() default Class.class;
/**
* Specifies which {@code SqlSessionTemplate} to use in the case that there is
* more than one in the spring context. Usually this is only needed when you
* have more than one datasource.
*/
String sqlSessionTemplateRef() default "";
/**
* Specifies which {@code SqlSessionFactory} to use in the case that there is
* more than one in the spring context. Usually this is only needed when you
* have more than one datasource.
*/
String sqlSessionFactoryRef() default "";
/**
* Specifies a custom MapperFactoryBean to return a mybatis proxy as spring bean.
*
*/
Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;
}
MapperScan注解属性如下:
- value、basePackages、basePackageClasses都是用于提供扫描接口类的包名;
- BeanNameGenerator作为Bean名称生成器,支持自定义;
- annotationClass、markerInterface作为marker标记,限定需要扫描的接口需要满足的条件(包含特定注解或实现某个接口);
- sqlSessionTemplateRef、sqlSessionFactoryRef在有多个数据源的情况下指定使用哪一个数据源;
- factoryBean,指定FactoryBean实现类,用于生成接口代理类,默认为MapperFactoryBean.class, 支持自定义;
另外MapperScan通过import注解导入了MapperScannerRegistrar类,该接口实现了ImportBeanDefinitionRegistrar接口,用于注册BeanDefinition;
2. MapperScannerRegistrar
ImportBeanDefinitionRegistrar:是spring开放出来的用于注册其他BeanDefinition的接口,供子类具体实现;
MapperScannerRegistrar:实现ImportBeanDefinitionRegistrar接口,同时依赖ClassPathMapperScanner获取候选bean定义, 并对bean定义进行后处理;
ClassPathMapperScanner:继承ClassPathBeanDefinitionScanner,完成指定包下bean定义的注册;
(传送门 Spring源码:ClassPathBeanDefinitionScanner源码分析)
覆盖的ImportBeanDefinitionRegistrar接口中的registerBeanDefinitions方法具体如下,主要完成了ClassPathMapperScanner的构造,并执行doScan方法进行扫描;
MapperScannerRegistrar.registerBeanDefinitions:
代码语言:javascript复制@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
// this check is needed in Spring 3.1
if (resourceLoader != null) {
scanner.setResourceLoader(resourceLoader);
}
Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");
if (!Annotation.class.equals(annotationClass)) {
scanner.setAnnotationClass(annotationClass);
}
Class<?> markerInterface = annoAttrs.getClass("markerInterface");
if (!Class.class.equals(markerInterface)) {
scanner.setMarkerInterface(markerInterface);
}
Class<? extends BeanNameGenerator> generatorClass = annoAttrs.getClass("nameGenerator");
if (!BeanNameGenerator.class.equals(generatorClass)) {
scanner.setBeanNameGenerator(BeanUtils.instantiateClass(generatorClass));
}
Class<? extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean");
if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass));
}
scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef"));
scanner.setSqlSessionFactoryBeanName(annoAttrs.getString("sqlSessionFactoryRef"));
List<String> basePackages = new ArrayList<String>();
for (String pkg : annoAttrs.getStringArray("value")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (String pkg : annoAttrs.getStringArray("basePackages")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (Class<?> clazz : annoAttrs.getClassArray("basePackageClasses")) {
basePackages.add(ClassUtils.getPackageName(clazz));
}
scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(basePackages));
}
3.ClassPathMapperScanner
ClassPathMapperScanner主要用于注册指定包名下的bean定义,并对注册的bean定义进行后处理,这里主要是设置beanClass为MapperFactoryBean、设置构造函数参数对象、设置autoWireMode=byType(MapperScan注解属性没有指定时,默认将容器中的DefalultSqlSectionFactory绑定到set方法的入参对象中,构造PropertyValue值),其doScan方法如下:
代码语言:javascript复制/**
* Calls the parent search that will search and register all the candidates.
* Then the registered objects are post processed to set them as
* MapperFactoryBeans
*/
@Override
public Set<BeanDefinitionHolder> doScan(String... basePackages) {
Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
if (beanDefinitions.isEmpty()) {
logger.warn("No MyBatis mapper was found in '" Arrays.toString(basePackages) "' package. Please check your configuration.");
} else {
processBeanDefinitions(beanDefinitions);
}
return beanDefinitions;
}
private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
GenericBeanDefinition definition;
for (BeanDefinitionHolder holder : beanDefinitions) {
definition = (GenericBeanDefinition) holder.getBeanDefinition();
if (logger.isDebugEnabled()) {
logger.debug("Creating MapperFactoryBean with name '" holder.getBeanName()
"' and '" definition.getBeanClassName() "' mapperInterface");
}
// the mapper interface is the original class of the bean
// but, the actual class of the bean is MapperFactoryBean
definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59
definition.setBeanClass(this.mapperFactoryBean.getClass());
definition.getPropertyValues().add("addToConfig", this.addToConfig);
boolean explicitFactoryUsed = false;
if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
explicitFactoryUsed = true;
} else if (this.sqlSessionFactory != null) {
definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
explicitFactoryUsed = true;
}
if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
if (explicitFactoryUsed) {
logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
}
definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
explicitFactoryUsed = true;
} else if (this.sqlSessionTemplate != null) {
if (explicitFactoryUsed) {
logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
}
definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
explicitFactoryUsed = true;
}
if (!explicitFactoryUsed) {
if (logger.isDebugEnabled()) {
logger.debug("Enabling autowire by type for MapperFactoryBean with name '" holder.getBeanName() "'.");
}
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
}
}
}
4. MapperFactoryBean
MapperFactoryBean实现FactoryBean接口,继承SqlSessionDaoSupport,其中SqlSessionDaoSupport有成员变量sqlSession,初始化是在setSqlSessionFactory方法中完成的(setSqlSessionFactory是根据autoWire=byType属性注入进来的)
SqlSessionDaoSupport.setSqlSessionFactory:
代码语言:javascript复制public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (!this.externalSqlSession) {
this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
}
}
因为MapperFactoryBean是FactoryBean的子类,所以bean实例化时会调用MapperFactoryBean的getObject方法:
代码语言:javascript复制@Override
public T getObject() throws Exception {
return getSqlSession().getMapper(this.mapperInterface);
}
进而调用sqlSession的getMapper接口,即是sqlSessionTemplete的getMapper方法:
代码语言:javascript复制@Override
public <T> T getMapper(Class<T> type) {
return getConfiguration().getMapper(type, this);
}
这样最终就从Configuration中获取mapper接口的代理对象;而Configuration中的mapper接口代理对象是在mybatis配置解析时构造完成的;
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/141940.html原文链接:https://javaforall.cn