SpringBoot-Loader源码分析系列1:启动&读取MANIFEST.MF文件

jdb org.springframework.boot.loader.JarLauncher
stop in org.springframework.boot.loader.JarLauncher.main
run

我们先看看

new JarLauncher() 的启动过程。

-----------------------------------------

在这个对象构造过程中,比较重要的一个函数是

this.archive = createArchive();

那么这个函数到底执行了什么呢?

我把debug过程中的每个值都打印出来了,如下

protected final Archive createArchive() throws Exception {
		//
		//
		//main[1] print protectionDomain
		// protectionDomain = "ProtectionDomain  (file:/root/springboot_tomcat_jetty_zipkin_brave_debug/ <no signer certificates>)
		// sun.misc.Launcher$AppClassLoader@18b4aac2
		// <no principals>
		 //java.security.Permissions@73a8dfcc (
		// ("java.io.FilePermission" "/root/springboot_tomcat_jetty_zipkin_brave_debug/-" "read")
		// ("java.lang.RuntimePermission" "exitVM")
		//)
		ProtectionDomain protectionDomain = getClass().getProtectionDomain();
		//
		//main[1] print codeSource
		//codeSource = "(file:/root/springboot_tomcat_jetty_zipkin_brave_debug/ <no signer certificates>)"
		CodeSource codeSource = protectionDomain.getCodeSource();
		//
		//main[1] print location
		// location = "file:/root/springboot_tomcat_jetty_zipkin_brave_debug/"
		URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
		//
		//main[1] print path
		//path = "/root/springboot_tomcat_jetty_zipkin_brave_debug/"
		String path = (location == null ? null : location.getSchemeSpecificPart());
		if (path == null) {
			throw new IllegalStateException("Unable to determine code source archive");
		}
		//
		//main[1] print root
		//root = "/root/springboot_tomcat_jetty_zipkin_brave_debug"
		File root = new File(path);
		
		if (!root.exists()) {
			throw new IllegalStateException(
					"Unable to determine code source archive from " + root);
		}
		//
		return (root.isDirectory() ? new ExplodedArchive(root)
				: new JarFileArchive(root));
	}

---接下来,我们看到最后一行是有2个分支,

其实就是说这个类是哪个哪个地方来的,目录就走分支1,jar包就走分支2,

平时是从jar包,我这里是从debug文件夹来的,所有就走分支1,下面开始debug分支1.

反正原理都是大差不离的。

===接下来,在构造ExplodedArchive对象的过程中,最重要的细节是读取MANIFEST文件,

见源码:

/**
	 * Create a new {@link ExplodedArchive} instance.
	 * @param root the root folder
	 * @param recursive if recursive searching should be used to locate the manifest.
	 * Defaults to {@code true}, folders with a large tree might want to set this to {code
	 * false}.
	 */
	public ExplodedArchive(File root, boolean recursive) {
		//
		if (!root.exists() || !root.isDirectory()) {
			throw new IllegalArgumentException("Invalid source folder " + root);
		}
		//
		this.root = root;
		this.recursive = recursive;
		//
		this.manifestFile = getManifestFile(root);
	}

	private File getManifestFile(File root) {
		File metaInf = new File(root, "META-INF");
		return new File(metaInf, "MANIFEST.MF");
	}

这个就不用解释了吧,已经拿到了MANIFEST.MF文件。

直接复制这个文件的内容,如下所示:

Manifest-Version: 1.0
Built-By: travis
Start-Class: zipkin.server.ZipkinServer
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 1.4.3.RELEASE
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_111
Main-Class: org.springframework.boot.loader.JarLauncher

然后就正式进入了new JarLauncher().launch(args);的.launch(args)部分,放在下一节讲解。

转载于:https://my.oschina.net/qiangzigege/blog/819496

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值