motan源码分析二:使用spi机制进行类加载
在motan的源码中使用了很多的spi机制进行对象的创建,下面我们来具体分析一下它的实现方法。
1.在实际的jar包的\META-INF\services目录中引入相关的文件,例如下图中,我解压了core的jar文件后,获得到的相应文件列表:

2.以第一节中的ConfigHandler为例来分析,打开上图中的com.weibo.api.motan.config.handler.ConfigHandler文件,文件内容标识着ConfigHandler接口的实现类为:com.weibo.api.motan.config.handler.SimpleConfigHandler
#
# Copyright 2009-2016 Weibo, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# com.weibo.api.motan.config.handler.SimpleConfigHandler
3.在第一节中,创建ConfigHandler对象的代码是这样的:
ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);
4.开始进入到实际的加载代码核心部分,首先来看一下类加载器的具体实现:
public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
checkInterfaceType(type);//基础性检查
ExtensionLoader<T> loader = (ExtensionLoader<T>) extensionLoaders.get(type);//之前是否已经加载过此加载器
if (loader == null) {
loader = initExtensionLoader(type);//第一次加载
}
return loader;
}
private static <T> void checkInterfaceType(Class<T> clz) {
if (clz == null) {
failThrows(clz, "Error extension type is null");
}
if (!clz.isInterface()) {
failThrows(clz, "Error extension type is not interface");
}
if (!isSpiType(clz)) {
failThrows(clz, "Error extension type without @Spi annotation");
}
}
public static synchronized <T> ExtensionLoader<T> initExtensionLoader(Class<T> type) {
ExtensionLoader<T> loader = (ExtensionLoader<T>) extensionLoaders.get(type);
if (loader == null) {
loader = new ExtensionLoader<T>(type);//新创建一个加载器
extensionLoaders.putIfAbsent(type, loader);
loader = (ExtensionLoader<T>) extensionLoaders.get(type);
}
return loader;
}
5.下面我们将进入到加载器的内部,分析具体的实现:
private ExtensionLoader(Class<T> type) {
this(type, Thread.currentThread().getContextClassLoader());//使用当前线程的类加载器做为加载器,type为ConfigHandler接口
}
public T getExtension(String name) {
checkInit();//检查是否初始化
if (name == null) {
return null;
}
try {
Spi spi = type.getAnnotation(Spi.class);
if (spi.scope() == Scope.SINGLETON) {
return getSingletonInstance(name);//返回唯一的对象
} else {
Class<T> clz = extensionClasses.get(name);
if (clz == null) {
return null;
}
return clz.newInstance();//重新创建对象
}
} catch (Exception e) {
failThrows(type, "Error when getExtension " + name, e);
}
return null;
}
private synchronized void loadExtensionClasses() {
if (init) {
return;
}
extensionClasses = loadExtensionClasses(PREFIX);//加载相关的类
singletonInstances = new ConcurrentHashMap<String, T>();
init = true;
}
private ConcurrentMap<String, Class<T>> loadExtensionClasses(String prefix) {
String fullName = prefix + type.getName();//全名为:jar包名+\META-INF\services\com.weibo.api.motan.config.handler.ConfigHandler文件里的类
List<String> classNames = new ArrayList<String>();
try {
Enumeration<URL> urls;
if (classLoader == null) {
urls = ClassLoader.getSystemResources(fullName);
} else {
urls = classLoader.getResources(fullName);
}
if (urls == null || !urls.hasMoreElements()) {
return new ConcurrentHashMap<String, Class<T>>();
}
System.out.println("fullname:"+fullName);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
System.out.println("url:"+url.getFile());
parseUrl(type, url, classNames);
}
} catch (Exception e) {
throw new MotanFrameworkException(
"ExtensionLoader loadExtensionClasses error, prefix: " + prefix + " type: " + type.getClass(), e);
}
for(String classN : classNames){
System.out.println("class:"+classN);
}
return loadClass(classNames);
}
6.在parseUrl方法中进行文件的内容读取,并在loadClass中完成类的加载
private void parseUrl(Class<T> type, URL url, List<String> classNames) throws ServiceConfigurationError {
InputStream inputStream = null;
BufferedReader reader = null;
try {
inputStream = url.openStream();
reader = new BufferedReader(new InputStreamReader(inputStream, MotanConstants.DEFAULT_CHARACTER));
String line = null;
int indexNumber = 0;
while ((line = reader.readLine()) != null) {
indexNumber++;
parseLine(type, url, line, indexNumber, classNames);//读取到类的名称:com.weibo.api.motan.config.handler.SimpleConfigHandler
}
} catch (Exception x) {
failLog(type, "Error reading spi configuration file", x);
} finally {
try {
if (reader != null) {
reader.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException y) {
failLog(type, "Error closing spi configuration file", y);
}
}
}
private ConcurrentMap<String, Class<T>> loadClass(List<String> classNames) {
ConcurrentMap<String, Class<T>> map = new ConcurrentHashMap<String, Class<T>>();
for (String className : classNames) {
try {
Class<T> clz;
if (classLoader == null) {
clz = (Class<T>) Class.forName(className);//装载类:com.weibo.api.motan.config.handler.SimpleConfigHandler
} else {
clz = (Class<T>) Class.forName(className, true, classLoader);
}
checkExtensionType(clz);
String spiName = getSpiName(clz);
if (map.containsKey(spiName)) {
failThrows(clz, ":Error spiName already exist " + spiName);
} else {
map.put(spiName, clz);
}
} catch (Exception e) {
failLog(type, "Error load spi class", e);
}
}
return map;
}
motan类加载的知识点总结:
1.使用jdk的spi规范,在\META-INF\services中添加实际的使用类描述,从而实现类与类之间的完全解耦;
2.类加载器使用的是当前线程的类加载器;
3.motan的类加载器可以支持单例和多例两种模式;
4.motan中大量使用了spi的类加载方式。
motan源码分析二:使用spi机制进行类加载的更多相关文章
- dubbo源码分析01:SPI机制
一.什么是SPI SPI全称为Service Provider Interface,是一种服务发现机制,其本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件.这样可以在运行时,动态为 ...
- Solr4.8.0源码分析(19)之缓存机制(二)
Solr4.8.0源码分析(19)之缓存机制(二) 前文<Solr4.8.0源码分析(18)之缓存机制(一)>介绍了Solr缓存的生命周期,重点介绍了Solr缓存的warn过程.本节将更深 ...
- 十、Spring之BeanFactory源码分析(二)
Spring之BeanFactory源码分析(二) 前言 在前面我们简单的分析了BeanFactory的结构,ListableBeanFactory,HierarchicalBeanFactory,A ...
- 多线程之美8一 AbstractQueuedSynchronizer源码分析<二>
目录 AQS的源码分析 该篇主要分析AQS的ConditionObject,是AQS的内部类,实现等待通知机制. 1.条件队列 条件队列与AQS中的同步队列有所不同,结构图如下: 两者区别: 1.链表 ...
- Java ArrayList源码分析(含扩容机制等重点问题分析)
写在最前面 这个项目是从20年末就立好的 flag,经过几年的学习,回过头再去看很多知识点又有新的理解.所以趁着找实习的准备,结合以前的学习储备,创建一个主要针对应届生和初学者的 Java 开源知识项 ...
- Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题
4.2 Fresco客户端与服务端的交互(一) 解决Q1问题 从这篇博客开始,我们开始讨论客户端与服务端是如何交互的,这个交互的入口,我们从Q1问题入手(博客按照这样的问题入手,是因为当时我也是从这里 ...
- Solr4.8.0源码分析(18)之缓存机制(一)
Solr4.8.0源码分析(18)之缓存机制(一) 前文在介绍commit的时候具体介绍了getSearcher()的实现,并提到了Solr的预热warn.那么本文开始将详细来学习下Solr的缓存机制 ...
- 框架-springmvc源码分析(二)
框架-springmvc源码分析(二) 参考: http://www.cnblogs.com/leftthen/p/5207787.html http://www.cnblogs.com/leftth ...
- java-通过 HashMap、HashSet 的源码分析其 Hash 存储机制
通过 HashMap.HashSet 的源码分析其 Hash 存储机制 集合和引用 就像引用类型的数组一样,当我们把 Java 对象放入数组之时,并非真正的把 Java 对象放入数组中.仅仅是把对象的 ...
随机推荐
- android自定义listview实现圆角
在项目中我们会经常遇到这种圆角效果,因为直角的看起来确实不那么雅观,可能大家会想到用图片实现,试想上中下要分别做三张图片,这样既会是自己的项目增大也会增加内存使用量,所以使用shape来实现不失为一种 ...
- MVC使用Exception过滤器自定义处理Action的的异常
1.继承FilterAttribute ,IExceptionFilter自定义处理 /// <summary> /// 登录错误自定义处理 /// </summary> pu ...
- 总结Linux下查看流量工具
Linux服务器要查看带宽情况,可以使用nethogs.dstat.nload.iftop.ifstat工具. 而每个工具都有自己的特色,这里简单总结一下使用方法. 一.nethogs 查看这台设备上 ...
- MySQL UPDATE
MySQL UPDATE 查询 如果我们需要修改或更新MySQL中的数据,我们可以使用 SQL UPDATE 命令来操作.. 语法 以下是 UPDATE 命令修改 MySQL 数据表数据的通用SQL语 ...
- linux术语解析(持续更新)
1.linux内核有个版本,分别是 longterm: 提供长期支持的内核版本 stable: 稳定版本 Beta 测试版
- 保垒机SSH登录脚本
线上服务器一般都会有一个保垒机,我们登录线上服务器的时候都要通过这个堡垒机来实现登录,这样我们就要维护很多线上服务器的ip,很麻烦. 所以写了一个脚本用来便捷的登录各个服务器,可以把这个脚本放到跳板机 ...
- SSM框架入门和搭建 十部曲
又快到毕业设计的时候了,有的学弟说想用ssm做毕业设计,在网上找到资料看不懂,基础差.我就帮他写了一个demo,顺便也整理一下. SSM框架,顾名思义,就是Spring+SpringMVC+mybat ...
- Ipad亚麻布纹背景-最终效果_学习教程
- Java学习----main详解
public class Test1 { public static void main(String[] args) { System.out.println("Hello"); ...
- python密码处理(可用于生产模式)
import os from hashlib import sha256 from hmac import HMAC def encrypt_password(password, salt=None) ...