热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springbootjar的启动原理解析

这篇文章主要介绍了springbootjar的启动原理分析,非常不错,具有参考借鉴价值,需要的朋友可以参考下

 1.前言

近来有空对公司的open api平台进行了些优化,然后在打出jar包的时候,突然想到以前都是对spring boot使用很熟练,但是从来都不知道spring boot打出的jar的启动原理,然后这回将jar解开了看了下,与想象中确实大不一样,以下就是对解压出来的jar的完整分析。

2.jar的结构

spring boot的应用程序就不贴出来了,一个较简单的demo打出的结构都是类似,另外我采用的spring boot的版本为1.4.1.RELEASE网上有另外一篇文章对spring boot jar启动的分析,那个应该是1.4以下的,启动方式与当前版本也有着许多的不同。

在mvn clean install后,我们在查看target目录中时,会发现两个jar包,如下:

xxxx.jar
xxx.jar.original

这个则是归功于spring boot插件的机制,将一个普通的jar打成了一个可以执行的jar包,而xxx.jar.original则是maven打出的jar包,这些可以参考spring官网的文章来了解,如下:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#executable-jar

以下是spring boot应用打出的jar的部分目录结构,大部分省略了,仅仅展示出其中重要的部分。

.
├── BOOT-INF
│ ├── classes
│ │ ├── application-dev.properties
│ │ ├── application-prod.properties
│ │ ├── application.properties
│ │ ├── com
│ │ │ └── weibangong
│ │ │  └── open
│ │ │   └── openapi
│ │ │    ├── SpringBootWebApplication.class
│ │ │    ├── config
│ │ │    │ ├── ProxyServletConfiguration.class
│ │ │    │ └── SwaggerConfig.class
│ │ │    ├── oauth2
│ │ │    │ ├── controller
│ │ │    │ │ ├── AccessTokenController.class
│ │ ├── logback-spring.xml
│ │ └── static
│ │  ├── css
│ │  │ └── guru.css
│ │  ├── images
│ │  │ ├── FBcover1200x628.png
│ │  │ └── NewBannerBOOTS_2.png
│ └── lib
│  ├── accessors-smart-1.1.jar
├── META-INF
│ ├── MANIFEST.MF
│ └── maven
│  └── com.weibangong.open
│   └── open-server-openapi
│    ├── pom.properties
│    └── pom.xml
└── org
 └── springframework
  └── boot
   └── loader
    ├── ExecutableArchiveLauncher$1.class
    ├── ExecutableArchiveLauncher.class
    ├── JarLauncher.class
    ├── LaunchedURLClassLoader$1.class
    ├── LaunchedURLClassLoader.class
    ├── Launcher.class
    ├── archive
    │ ├── Archive$Entry.class
    │ ├── Archive$EntryFilter.class
    │ ├── Archive.class
    │ ├── ExplodedArchive$1.class
    │ ├── ExplodedArchive$FileEntry.class
    │ ├── ExplodedArchive$FileEntryIterator$EntryComparator.class
     ├── ExplodedArchive$FileEntryIterator.class

这个jar除了我们写的应用程序打出的class以外还有一个单独的org包,应该是spring boot应用在打包的使用spring boot插件将这个package打进来,也就是增强了mvn生命周期中的package阶段,而正是这个包在启动过程中起到了关键的作用,另外中jar中将应用所需的各种依赖都打进来,并且打入了spring boot额外的package,这种可以all-in-one的jar也被称之为fat.jar,下文我们将一直以fat.jar来代替打出的jar的名字。

3.MANIFEST.MF文件

这个时候我们再继续看META-INF中的MANIFEST.MF文件,如下:

Manifest-Version: 1.0
Implementation-Title: open :: server :: openapi
Implementation-Version: 1.0-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: xiaxuan
Implementation-Vendor-Id: com.weibangong.open
Spring-Boot-Version: 1.4.1.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.PropertiesLauncher
Start-Class: com.weibangong.open.openapi.SpringBootWebApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_20
Implementation-URL: http://maven.apache.org/open-server-openapi

这里指定的main-class是单独打入的包中的一个类文件而不是我们的启动程序,然后MANIFEST.MF文件有一个单独的start-class指定的是我们的应用的启动程序。

4.启动分析

首先我们找到类org.springframework.boot.loader.PropertiesLauncher,其中main方法为:

public static void main(String[] args) throws Exception {
  PropertiesLauncher launcher = new PropertiesLauncher();
  args = launcher.getArgs(args);
  launcher.launch(args);
}

查看launch方法,这个方法在父类Launcher中,找到父类方法launch方法,如下:

 protected void launch(String[] args, String mainClass, ClassLoader classLoader) throws Exception {
  Thread.currentThread().setContextClassLoader(classLoader);
  this.createMainMethodRunner(mainClass, args, classLoader).run();
 }
 protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) {
  return new MainMethodRunner(mainClass, args);
 }

   launch方法最终调用了createMainMethodRunner方法,后者实例化了MainMethodRunner对象并运行了run方法,我们转到MainMethodRunner源码中,如下:

package org.springframework.boot.loader;
import java.lang.reflect.Method;
public class MainMethodRunner {
 private final String mainClassName;
 private final String[] args;
 public MainMethodRunner(String mainClass, String[] args) {
  this.mainClassName = mainClass;
  this.args = args == null?null:(String[])args.clone();
 }
 public void run() throws Exception {
  Class mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName);
  Method mainMethod = mainClass.getDeclaredMethod("main", new Class[]{String[].class});
  mainMethod.invoke((Object)null, new Object[]{this.args});
 }
}

查看run方法,就很怎么将spring boot的jar怎么运行起来的了,由此分析基本也就结束了。

5、main程序的启动流程

讲完了jar的启动流程,现在来讲下spring boot应用中,main程序的启动与加载流程,首先我们看一个spring boot应用的main方法。

package cn.com.devh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
 * Created by xiaxuan on 17/8/25.
 */
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class A1ServiceApplication {
 public static void main(String[] args) {
  SpringApplication.run(A1ServiceApplication.class, args);
 }
}

转到SpringApplication中的run方法,如下:

 /**
  * Static helper that can be used to run a {@link SpringApplication} from the
  * specified source using default settings.
  * @param source the source to load
  * @param args the application arguments (usually passed from a Java main method)
  * @return the running {@link ApplicationContext}
  */
 public static ConfigurableApplicationContext run(Object source, String... args) {
  return run(new Object[] { source }, args);
 }

 /**
  * Static helper that can be used to run a {@link SpringApplication} from the
  * specified sources using default settings and user supplied arguments.
  * @param sources the sources to load
  * @param args the application arguments (usually passed from a Java main method)
  * @return the running {@link ApplicationContext}
  */
 public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
  return new SpringApplication(sources).run(args);
 }

这里的SpringApplication的实例化是关键,我们转到SpringApplication的构造函数。

 /**
  * Create a new {@link SpringApplication} instance. The application context will load
  * beans from the specified sources (see {@link SpringApplication class-level}
  * documentation for details. The instance can be customized before calling
  * {@link #run(String...)}.
  * @param sources the bean sources
  * @see #run(Object, String[])
  * @see #SpringApplication(ResourceLoader, Object...)
  */
 public SpringApplication(Object... sources) {
  initialize(sources);
 }
 private void initialize(Object[] sources) {
  if (sources != null && sources.length > 0) {
   this.sources.addAll(Arrays.asList(sources));
  }
  this.webEnvirOnment= deduceWebEnvironment();
  setInitializers((Collection) getSpringFactoriesInstances(
    ApplicationContextInitializer.class));
  setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
  this.mainApplicatiOnClass= deduceMainApplicationClass();
 }

  这里的initialize方法中的deduceWebEnvironment()确定了当前是以web应用启动还是以普通的jar启动,如下:

 private boolean deduceWebEnvironment() {
  for (String className : WEB_ENVIRONMENT_CLASSES) {
   if (!ClassUtils.isPresent(className, null)) {
    return false;
   }
  }
  return true;
 }

其中的WEB_ENVIRONMENT_CLASSES为:  

 private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet",
   "org.springframework.web.context.ConfigurableWebApplicationContext" };

只要其中任何一个不存在,即当前应用以普通jar的形式启动。

然后setInitializers方法初始化了所有的ApplicationContextInitializer,

/**
  * Sets the {@link ApplicationContextInitializer} that will be applied to the Spring
  * {@link ApplicationContext}.
  * @param initializers the initializers to set
  */
 public void setInitializers(
   Collection<&#63; extends ApplicationContextInitializer<&#63;>> initializers) {
  this.initializers = new ArrayList>();
  this.initializers.addAll(initializers);
 }
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class))**

这一步初始化所有Listener。

我们再回到之前的SpringApplication(sources).run(args);处,进入run方法,代码如下:

/**
  * Run the Spring application, creating and refreshing a new
  * {@link ApplicationContext}.
  * @param args the application arguments (usually passed from a Java main method)
  * @return a running {@link ApplicationContext}
  */
 public ConfigurableApplicationContext run(String... args) {
  StopWatch stopWatch = new StopWatch();
  stopWatch.start();
  ConfigurableApplicationContext cOntext= null;
  configureHeadlessProperty();
  SpringApplicationRunListeners listeners = getRunListeners(args);
  listeners.started();
  try {
   ApplicationArguments applicatiOnArguments= new DefaultApplicationArguments(
     args);
   cOntext= createAndRefreshContext(listeners, applicationArguments);
   afterRefresh(context, applicationArguments);
   listeners.finished(context, null);
   stopWatch.stop();
   if (this.logStartupInfo) {
    new StartupInfoLogger(this.mainApplicationClass)
      .logStarted(getApplicationLog(), stopWatch);
   }
   return context;
  }
  catch (Throwable ex) {
   handleRunFailure(context, listeners, ex);
   throw new IllegalStateException(ex);
  }
 }

这一步进行上下文的创建createAndRefreshContext(listeners, applicationArguments),

private ConfigurableApplicationContext createAndRefreshContext(
   SpringApplicationRunListeners listeners,
   ApplicationArguments applicationArguments) {
  ConfigurableApplicationContext context;
  // Create and configure the environment
  ConfigurableEnvironment envirOnment= getOrCreateEnvironment();
  configureEnvironment(environment, applicationArguments.getSourceArgs());
  listeners.environmentPrepared(environment);
  if (isWebEnvironment(environment) && !this.webEnvironment) {
   envirOnment= convertToStandardEnvironment(environment);
  }
  if (this.bannerMode != Banner.Mode.OFF) {
   printBanner(environment);
  }
  // Create, load, refresh and run the ApplicationContext
  cOntext= createApplicationContext();
  context.setEnvironment(environment);
  postProcessApplicationContext(context);
  applyInitializers(context);
  listeners.contextPrepared(context);
  if (this.logStartupInfo) {
   logStartupInfo(context.getParent() == null);
   logStartupProfileInfo(context);
  }
  // Add boot specific singleton beans
  context.getBeanFactory().registerSingleton("springApplicationArguments",
    applicationArguments);
  // Load the sources
  Set sources = getSources();
  Assert.notEmpty(sources, "Sources must not be empty");
  load(context, sources.toArray(new Object[sources.size()]));
  listeners.contextLoaded(context);
  // Refresh the context
  refresh(context);
  if (this.registerShutdownHook) {
   try {
    context.registerShutdownHook();
   }
   catch (AccessControlException ex) {
    // Not allowed in some environments.
   }
  }
  return context;
 }
// Create and configure the environment
  ConfigurableEnvironment envirOnment= getOrCreateEnvironment();
  configureEnvironment(environment, applicationArguments.getSourceArgs());

这一步进行了环境的配置与加载。

  if (this.bannerMode != Banner.Mode.OFF) {
   printBanner(environment);
  }

这一步进行了打印spring boot logo,需要更改的话,在资源文件中加入banner.txt,banner.txt改为自己需要的图案即可。

// Create, load, refresh and run the ApplicationContext
  cOntext= createApplicationContext();
return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass)

创建上下文,这一步中真正包含了是创建什么容器,并进行了响应class的实例化,其中包括了EmbeddedServletContainerFactory的创建,是选择jetty还是tomcat,内容繁多,留待下一次再讲。

if (this.registerShutdownHook) {
   try {
    context.registerShutdownHook();
   }
   catch (AccessControlException ex) {
    // Not allowed in some environments.
   }
  }

这一步就是当前上下文进行注册,当收到kill指令的时候进行容器的销毁等工作了。

基本到此,启动的分析就结束了,但是还有一些细节讲述起来十分耗时,这个留待后续的博文中再来讲述,今天就到这里。

6.总结

综上spring boot jar的启动流程基本就是下面几个步骤:

1、我们正常进行maven打包时,spring boot插件扩展maven生命周期,将spring boot相关package打入到jar中,这个jar中包含了应用所打出的jar以外还有spring boot启动程序相关的类文件。

2、我以前看过稍微低一些版本的spring boot的jar的启动流程,当时我记得是当前线程又起了一个新线程来运行main程序,而现在的已经改成了直接使用反射来启动main程序。

总结

以上所述是小编给大家介绍的spring boot jar的启动原理解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


推荐阅读
  • 一句话解决高并发的核心原则
    本文介绍了解决高并发的核心原则,即将用户访问请求尽量往前推,避免访问CDN、静态服务器、动态服务器、数据库和存储,从而实现高性能、高并发、高可扩展的网站架构。同时提到了Google的成功案例,以及适用于千万级别PV站和亿级PV网站的架构层次。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了在Win10上安装WinPythonHadoop的详细步骤,包括安装Python环境、安装JDK8、安装pyspark、安装Hadoop和Spark、设置环境变量、下载winutils.exe等。同时提醒注意Hadoop版本与pyspark版本的一致性,并建议重启电脑以确保安装成功。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 标题: ... [详细]
  • 本文介绍了一些Java开发项目管理工具及其配置教程,包括团队协同工具worktil,版本管理工具GitLab,自动化构建工具Jenkins,项目管理工具Maven和Maven私服Nexus,以及Mybatis的安装和代码自动生成工具。提供了相关链接供读者参考。 ... [详细]
  • 本文介绍了一种图片处理应用,通过固定容器来实现缩略图的功能。该方法可以实现等比例缩略、扩容填充和裁剪等操作。详细的实现步骤和代码示例在正文中给出。 ... [详细]
  • 本文分析了Wince程序内存和存储内存的分布及作用。Wince内存包括系统内存、对象存储和程序内存,其中系统内存占用了一部分SDRAM,而剩下的30M为程序内存和存储内存。对象存储是嵌入式wince操作系统中的一个新概念,常用于消费电子设备中。此外,文章还介绍了主电源和后备电池在操作系统中的作用。 ... [详细]
author-avatar
lao6345790
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有