Mybatis源码解析,一步一步从浅入深(六):映射代理类的获取
在文章:Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码中我们提到了两个问题:
1,为什么在以前的代码流程中从来没有addMapper,而这里却有getMapper?
2,UserDao明明是我们定义的一个接口类,根本没有定义实现类,那这个userMapper是什么?是mybatis自动为我们生成的实现类吗?
为了更好的解释着两个问题,我们需要重新认识Configuration这个类。
但是在这之前,你需要了解一个概念(设计模式):JAVA设计模式-动态代理(Proxy)示例及说明。否则你可能对接下来的流程一头雾水。
一,再次认识Configuration
- public class Configuration {
- //映射注册表
- protected MapperRegistry mapperRegistry = new MapperRegistry(this);
- // 获取映射注册表
- public MapperRegistry getMapperRegistry() {
- return mapperRegistry;
- }
- //添加到映射注册表
- public void addMappers(String packageName, Class<?> superType) {
- mapperRegistry.addMappers(packageName, superType);
- }
- //添加到映射注册表
- public void addMappers(String packageName) {
- mapperRegistry.addMappers(packageName);
- }
- //添加到映射注册表
- public <T> void addMapper(Class<T> type) {
- mapperRegistry.addMapper(type);
- }
- //从映射注册表中获取
- public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
- return mapperRegistry.getMapper(type, sqlSession);
- }
- //判断映射注册表中是否存在
- public boolean hasMapper(Class<?> type) {
- return mapperRegistry.hasMapper(type);
- }
- }
MapperRegistry源码:
- public class MapperRegistry {
- private Configuration config;
- //映射缓存 键:类对象,值:映射代理工厂
- private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
- public MapperRegistry(Configuration config) {
- this.config = config;
- }
- //从映射注册表中获取
- @SuppressWarnings("unchecked")
- 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);
- }
- }
- //判断映射注册表中是否存在
- public <T> boolean hasMapper(Class<T> type) {
- return knownMappers.containsKey(type);
- }
- //添加到映射注册表
- public <T> void addMapper(Class<T> type) {
- if (type.isInterface()) {
- if (hasMapper(type)) {
- throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
- }
- boolean loadCompleted = false;
- try {
- knownMappers.put(type, new MapperProxyFactory<T>(type));
- // It's important that the type is added before the parser is run
- // otherwise the binding may automatically be attempted by the
- // mapper parser. If the type is already known, it won't try.
- // 处理接口类(例如UserDao)中的注解
- MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
- parser.parse();
- loadCompleted = true;
- } finally {
- if (!loadCompleted) {
- knownMappers.remove(type);
- }
- }
- }
- }
- /**
- * @since 3.2.2
- */
- //获取缓存中所有的Key,并且是不可修改的
- public Collection<Class<?>> getMappers() {
- return Collections.unmodifiableCollection(knownMappers.keySet());
- }
- /**
- * @since 3.2.2
- */
- //添加到映射注册表
- public void addMappers(String packageName, Class<?> superType) {
- ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
- resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
- Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
- for (Class<?> mapperClass : mapperSet) {
- addMapper(mapperClass);
- }
- }
- /**
- * @since 3.2.2
- */
- //添加到映射注册表
- public void addMappers(String packageName) {
- addMappers(packageName, Object.class);
- }
- }
在方法getMappers中用到了Collections.unmodifiableCollection(knownMappers.keySet());,如果你不了解,可以查阅:Collections.unmodifiableMap,Collections.unmodifiableList,Collections.unmodifiableSet作用及源码解析
在了解了这两个类之后,就来解决第一个问题:1,为什么在以前的代码流程中从来没有addMapper,而这里却有getMapper?
二,addMapper和getMapper
1,关于addMapper,在文章Mybatis源码解析,一步一步从浅入深(五):mapper节点的解析中,不知道大家有没有意识到,我少了一个部分没有解读:
对了,就是第四部分的:绑定已经解析的命名空间
代码:bindMapperForNamespace();
是的,addMapper就是在这个方法中用到的。但是前提是,你需要了解java的动态代理。来看看源码:
- private void bindMapperForNamespace() {
- //获取当前命名空间(String:com.zcz.learnmybatis.dao.UserDao)
- String namespace = builderAssistant.getCurrentNamespace();
- if (namespace != null) {
- Class<?> boundType = null;
- try {
- // 使用类加载器加载,加载类,获取类对象
- boundType = Resources.classForName(namespace);
- } catch (ClassNotFoundException e) {
- //ignore, bound type is not required
- }
- if (boundType != null) {
- //判断映射注册表中是否存在
- if (!configuration.hasMapper(boundType)) {
- // Spring may not know the real resource name so we set a flag
- // to prevent loading again this resource from the mapper interface
- // look at MapperAnnotationBuilder#loadXmlResource
- // 添加到已经解析的缓存
- configuration.addLoadedResource("namespace:" + namespace);
- // 添加到映射这测表
- configuration.addMapper(boundType);
- }
- }
- }
- }
看到了吧,就在最后一行代码。但是这里并不是简单的保存了一个类对象,而是在MapperRegistry中进行了进一步的处理:
- //添加到映射注册表
- public <T> void addMapper(Class<T> type) {
- if (type.isInterface()) {
- if (hasMapper(type)) {
- throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
- }
- boolean loadCompleted = false;
- try {
- // 在这里,保存的是new MapperProxyFactory实例对象。
- knownMappers.put(type, new MapperProxyFactory<T>(type));
- // It's important that the type is added before the parser is run
- // otherwise the binding may automatically be attempted by the
- // mapper parser. If the type is already known, it won't try.
- // 处理接口类(例如UserDao)中的注解
- MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
- parser.parse();
- loadCompleted = true;
- } finally {
- if (!loadCompleted) {
- knownMappers.remove(type);
- }
- }
- }
- }
在第10行,保存的是映射代理工厂(MapperProxyFactory)的实例对象。到这里addMapper就解释清楚了。接下来看看getMapper方法。
2,getMapper
调用的地方:在文章Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码第三步中。
代码:configuration.<T>getMapper(type, this);
type:是UserDao.class
this:SqlSession的实例化对象
从第一部分Configuration中可以发现,Configuration又调用了MapperRegistry的getMapper方法
- //从映射注册表中获取
- @SuppressWarnings("unchecked")
- 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);
- }
- }
从代码的第4行可以清晰的看到,或者根据类对象从缓存Map中,获取到了addMapper中保存的MapperProxyFactory对象实例。但是并没有将这个对象实例直接返回,而是通过调用的MapperProxyFactory的newInstance方法返回的一个UserDao实现类。接下来我们i就解释一下第二个问题:2,UserDao明明是我们定义的一个接口类,根本没有定义实现类,那这个userMapper是什么?是mybatis自动为我们生成的实现类吗?
三,映射代理类的实现
看过JAVA设计模式-动态代理(Proxy)示例及说明这篇文章的同学应该知道这个问题的答案了,userMapper是一个代理类对象实例。是通过映射代理工厂(MapperProxyFactory)的方法newInstance方法获取的。
但是在这里mybatis有一个很巧妙的构思,使得这个的动态代理的使用方法和文章JAVA设计模式-动态代理(Proxy)示例及说明中的使用方法有些许不同。
不妨在你的脑海中回顾一下JAVA设计模式-动态代理(Proxy)示例及说明中实现动态代理的关键因素:
1,一个接口
2,实现了接口的类
3,一个调用处理类(构造方法中要传入2中的类的实例对象)
4,调用Proxy.newProxyInstance方法获取代理类实例化对象
带着这个印象,我们来分析一下mybatis是怎么实现动态代理的。既然userMapper是通过映射代理工厂(MapperProxyFactory)生产出来的,那么我们就看看它的源码:
- //映射代理工厂
- public class MapperProxyFactory<T> {
- // 接口类对象(UserDao.class)
- private final Class<T> mapperInterface;
- // 对象中的方法缓存
- private Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();
- //构造器
- public MapperProxyFactory(Class<T> mapperInterface) {
- //为接口类对象赋值
- this.mapperInterface = mapperInterface;
- }
- public Class<T> getMapperInterface() {
- return mapperInterface;
- }
- public Map<Method, MapperMethod> getMethodCache() {
- return methodCache;
- }
- // 实例化映射代理类
- @SuppressWarnings("unchecked")
- 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);
- }
- }
我们调用的newInstance方法就是第29行的方法。然后这个方法又调用了24行的方法。我们来看25行的代码,是不是很熟悉?Proxy.newProxyInstance(类加载器,接口类对象数组,实现了InvocationHandler的对象实例 mapperProxy),到这里映射代理类的实例化已经解释清楚了,也就是解决了第二个问题,接下来我们扩展一下:
现在我们还没有看到MapperProxy类的源码,但是我们可以大胆的猜测,类MapperProxy一定是实现了InvocationHandler接口,并且也一定实现了Invoke方法:
- // 映射代理
- public class MapperProxy<T> implements InvocationHandler, Serializable {
- private static final long serialVersionUID = -6424540398559729838L;
- private final SqlSession sqlSession;
- private final Class<T> mapperInterface;
- private final Map<Method, MapperMethod> methodCache;
- //构造方法
- public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
- this.sqlSession = sqlSession;
- this.mapperInterface = mapperInterface;
- this.methodCache = methodCache;
- }
- //代理类调用的时候执行的方法
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- // 检查Method方法所在的类是否是Object
- if (Object.class.equals(method.getDeclaringClass())) {
- try {
- return method.invoke(this, args);
- } catch (Throwable t) {
- throw ExceptionUtil.unwrapThrowable(t);
- }
- }
- // 应用缓存
- final MapperMethod mapperMethod = cachedMapperMethod(method);
- //执行查询
- return mapperMethod.execute(sqlSession, args);
- }
- //应用缓存
- private MapperMethod cachedMapperMethod(Method method) {
- MapperMethod mapperMethod = methodCache.get(method);
- if (mapperMethod == null) {
- mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
- methodCache.put(method, mapperMethod);
- }
- return mapperMethod;
- }
- }
那么,mybatis使用动态代理的方式跟文章JAVA设计模式-动态代理(Proxy)示例及说明使用动态代理的方式,有哪些不同呢?
1,一个接口(UserDao)
2,实现了接口的类(没有)
3,一个调用处理类(构造方法中要传入2中的类的实例对象)(2中没有,自然这里也不会传入对象)
4,调用Proxy.newProxyInstance方法获取代理类实例化对象(有的)
如果你实实在在的明白了JAVA设计模式-动态代理(Proxy)示例及说明这篇文中所叙述的内容,相信这篇文章不难理解。
好了,mybatis源码解析到这里,已经基本接近尾声了,继续探索吧:Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码
原创不易,转载请声明出处:https://www.cnblogs.com/zhangchengzi/p/9706395.html
Mybatis源码解析,一步一步从浅入深(六):映射代理类的获取的更多相关文章
- Mybatis源码解析,一步一步从浅入深(一):创建准备工程
Spring SpringMVC Mybatis(简称ssm)是一个很流行的java web框架,而Mybatis作为ORM 持久层框架,因其灵活简单,深受青睐.而且现在的招聘职位中都要求应试者熟悉M ...
- Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码
在文章:Mybatis源码解析,一步一步从浅入深(一):创建准备工程,中我们为了解析mybatis源码创建了一个mybatis的简单工程(源码已上传github,链接在文章末尾),并实现了一个查询功能 ...
- Mybatis源码解析,一步一步从浅入深(三):实例化xml配置解析器(XMLConfigBuilder)
在上一篇文章:Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码 ,中我们看到 代码:XMLConfigBuilder parser = new XMLConfigBuilder(read ...
- Mybatis源码解析,一步一步从浅入深(四):将configuration.xml的解析到Configuration对象实例
在Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码中我们看到了XMLConfigBuilder(xml配置解析器)的实例化.而且这个实例化过程在文章:Mybatis源码解析,一步一步从浅 ...
- Mybatis源码解析,一步一步从浅入深(五):mapper节点的解析
在上一篇文章Mybatis源码解析,一步一步从浅入深(四):将configuration.xml的解析到Configuration对象实例中我们谈到了properties,settings,envir ...
- Mybatis源码解析,一步一步从浅入深(七):执行查询
一,前言 我们在文章:Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码的最后一步说到执行查询的关键代码: result = sqlSession.selectOne(command.ge ...
- 【MyBatis源码解析】MyBatis一二级缓存
MyBatis缓存 我们知道,频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级),尤其是对于一些相 ...
- Mybatis源码解析-DynamicSqlSource和RawSqlSource的区别
XMLLanguageDriver是ibatis的默认解析sql节点帮助类,其中的方法其会调用生成DynamicSqlSource和RawSqlSource这两个帮助类,本文将对此作下简单的简析 应用 ...
- Mybatis源码解析(四) —— SqlSession是如何实现数据库操作的?
Mybatis源码解析(四) -- SqlSession是如何实现数据库操作的? 如果拿一次数据库请求操作做比喻,那么前面3篇文章就是在做请求准备,真正执行操作的是本篇文章要讲述的内容.正如标题一 ...
随机推荐
- idea实现第一个springboot程序
1.环境准备 JDK:1.8 Apache Maven: 3.6.1 IntelliJ IDEA 2019.1.3 x64 SpringBoot 1.5.9.RELEASE:1.5.9: 1.1.MA ...
- Leetcode solution 124: Binary Tree Maximum Path Sum
Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path ...
- java 中 size() 和 length()
偶然发现自己不清楚 java size() 和length()是干嘛用的,总结一下: 1.java中的length()方法是针对字符串String说的,如果想看这个字符串的长度则用到length()这 ...
- RabbitMQ的各个参数
简介 原文:https://blog.csdn.net/vbirdbest/article/details/78670550 本节主要讨论队列声明的各个参数 queueDeclare(String q ...
- Windows内核原理-同步IO与异步IO
目录 Windows内核原理-同步IO与异步IO 背景 目的 I/O 同步I/O 异步I/O I/O完成通知 总结 参考文档 Windows内核原理-同步IO与异步IO 背景 在前段时间检查异常连接导 ...
- UVA 10395 素数筛
Twin Primes Twin primes are pairs of primes of the form (p; p + 2). The term \twin prime" was c ...
- Azure DevOps vsts-agent-linux 安装出错, Must not run with sudo
在linux安装 vsts-agent-linux 在vsts-agent-linux的解压目录运行./config.sh, 提示"Must not run with sudo", ...
- requests.get爬虫模块参数
地址和请求头参数--url和header res = requests.get(url,headers=headers) 向网站发起请求,并获取响应对象 参数 url :需要抓取的URL地址 head ...
- HTML连载34-背景关联和缩写以及插图图片和背景图片的区别
一.背景属性缩写的格式 1.backgound:背景颜色 背景图片 平铺方式 关联方式 定位方式 2.注意点: 这里的所有值都可以省略,但是至少需要一个 3.什么是背景关联方式 默认情况下,背 ...
- volatile、Synchronized实现变量可见性的原理,volatile使用注意事项
变量不可见的两个原因 Java每个线程工作都有一个工作空间,需要的变量都是从主存中加载进来的.Java内存模型如下(JMM): 线程访问一个共享的变量时,都需要先从主存中加载一个副本到自己的工作内存中 ...