SpringBoot自动装配 - 持久层框架Mybatis(MybatisAutoConfiguration)


记录学习过程笔记

如果本文有任何错误,请批评指教,不胜感激 !

自动装配:MybatisAutoConfiguration

1、Mapper.xml处理

  1. 根据配置文件里配置的路径mybatis.mapper-locations,扫描匹配的文件(Mapper.xml)
package org.mybatis.spring.boot.autoconfigure;

public class MybatisAutoConfiguration {

	// Mybatis配置属性
	private final MybatisProperties properties;
	
	@Bean
  	@ConditionalOnMissingBean
  	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
		if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
      		factory.setMapperLocations(this.properties.resolveMapperLocations());
    	}
	}
}
  1. 读取Mapper.xml配置文件资源,存储位置:mapperLocations
  2. 解析mapperLocations,对应Mapper.xml配置文件中的一个select/update/insert/delete节点,描述的就是一条SQL语句,key=namespace + “.” + id,存储位置:mappedStatements;
package org.mybatis.spring;

public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
	private Resource[] mapperLocations;
	
	protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
		if (!isEmpty(this.mapperLocations)) {
      		for (Resource mapperLocation : this.mapperLocations) {
      			// 省略
      		}
      	}
		
		
		return this.sqlSessionFactoryBuilder.build(configuration);
	}
}
package org.apache.ibatis.session;

public class Configuration {
	  protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");
}
  1. 根据namespace,获取Mapper接口的Class对象作为key,生成对应Mapper代理工厂,存储位置:knownMappers
package org.apache.ibatis.session;

public class Configuration {
	protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
}
package org.apache.ibatis.binding;

public class MapperRegistry {
	  private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
}

2、Mapper接口类处理

  1. 如果未使用注解@MapperScan,则把MapperScannerRegistrarNotFoundConfiguration实例和AutoConfiguredMapperScannerRegistrar实例加入Spring的Ioc容器
package org.mybatis.spring.boot.autoconfigure;

public class MybatisAutoConfiguration {

  @org.springframework.context.annotation.Configuration
  @Import({ AutoConfiguredMapperScannerRegistrar.class })
  @ConditionalOnMissingBean(MapperFactoryBean.class)
  public static class MapperScannerRegistrarNotFoundConfiguration {

    @PostConstruct
    public void afterPropertiesSet() {
      logger.debug("No {} found.", MapperFactoryBean.class.getName());
    }
  }

}
  1. 使用注解@MapperScan,则把MapperScannerRegistrar实例加入Spring的Ioc容器
package org.mybatis.spring.annotation;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {}
  1. 通过ClassPathMapperScanner为每个mapper接口类创建对应的BeanDefinition实例
public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
 	@Override
  	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
	}
}

存储位置:beanDefinitionMap

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
		implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
		
	// Bean定义信息
	private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

}
  1. BeanDefinition的beanClass重新设置为MapperFactoryBean,autoWireMode为byType
package org.mybatis.spring.mapper;

public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner {
	private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
		// 省略
		definition.setBeanClass(this.mapperFactoryBean.getClass());
		// 省略
		definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
		// 省略
	}
}

3、Mapper接口类依赖注入(bean实例化过程)

  1. 因为在处理Mapper接口类的BeanDefinition时,beanClass重新设置为MapperFactoryBean,所以属性注入的实例化的是MapperFactoryBean#getObject
package org.mybatis.spring.mapper;

public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
	
	@Override
  	public T getObject() throws Exception {
    	return getSqlSession().getMapper(this.mapperInterface);
  	}
}

  1. 通过Mapper代理工厂创建实例
package org.apache.ibatis.session;

public class Configuration {

	protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
	
	public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }
}
package org.apache.ibatis.binding;

public class MapperRegistry {

	public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    	final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    	if (mapperProxyFactory == null) {
      		throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    	}
    	try {
      	return mapperProxyFactory.newInstance(sqlSession);
    	} catch (Exception e) {
      		throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    	}
  	}
}
  1. 返回代理后的MapperProxy实例,作为Mapper接口类的实例进行注入
package org.apache.ibatis.binding;

public class MapperProxyFactory<T> {
	protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }
}
  1. 当我们使用Mapper接口类的声明时,实际执行的MapperProxy#invoke方法
package org.apache.ibatis.binding;

public class MapperProxy<T> implements InvocationHandler, Serializable {
	@Override
  	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    	try {
      		if (Object.class.equals(method.getDeclaringClass())) {
        		return method.invoke(this, args);
      		} else if (isDefaultMethod(method)) {
        		return invokeDefaultMethod(proxy, method, args);
      		}
    	} catch (Throwable t) {
      		throw ExceptionUtil.unwrapThrowable(t);
    	}
    	final MapperMethod mapperMethod = cachedMapperMethod(method);
    	return mapperMethod.execute(sqlSession, args);
  	}
}

4、实例化SqlSessionFactory加入Spring的Ioc容器

package org.mybatis.spring;

public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {

   private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

   protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
   		// 省略
   		return this.sqlSessionFactoryBuilder.build(configuration);
   }
}
package org.apache.ibatis.session;

public class SqlSessionFactoryBuilder {
	public SqlSessionFactory build(Configuration config) {
    	return new DefaultSqlSessionFactory(config);
  	}
}

5、实例化SqlSessionTemplate加入Spring的Ioc容器

package org.mybatis.spring.boot.autoconfigure;

public class MybatisAutoConfiguration {

	@Bean
  	@ConditionalOnMissingBean
  	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    	ExecutorType executorType = this.properties.getExecutorType();
    	if (executorType != null) {
      		return new SqlSessionTemplate(sqlSessionFactory, executorType);
    	} else {
      		return new SqlSessionTemplate(sqlSessionFactory);
    	}
  	}
}

记录学习过程笔记

如果本文有任何错误,请批评指教,不胜感激 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值