springboot应用启动原理分析_第1页
springboot应用启动原理分析_第2页
springboot应用启动原理分析_第3页
springboot应用启动原理分析_第4页
springboot应用启动原理分析_第5页
已阅读5页,还剩28页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、33/33spring boot quick start在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启动的,不需要另外配置一个Web Server。如果之前没有使用过spring boot可以通过下面的demo来感受下。下面以这个工程为例,演示如何启动Spring boot项目:git clone :hengyunabc/spring-boot-demo.gitmvn spring-boot-demojava -jar target/demo-0.0.1-SNAPSHOT.jar123如果使用的IDE是spring

2、sts或者idea,可以通过向导来创建spring boot项目。也可以参考官方教程: HYPERLINK l getting-started-first-application t _blank 对spring boot的两个疑问刚开始接触spring boot时,通常会有这些疑问spring boot如何启动的?spring boot embed tomcat是如何工作的? 静态文件,jsp,网页模板这些是如何加载到的?下面来分析spring boot是如何做到的。打包为单个jar时,spring boot的启动方式maven打包之后,会生成两个jar文件:demo-0.0.1-SNAPS

3、HOT.jardemo-0.0.1-SNAPSHOT.jar.original12其中demo-0.0.1-SNAPSHOT.jar.original是默认的maven-jar-plugin生成的包。demo-0.0.1-SNAPSHOT.jar是spring boot maven插件生成的jar包,里面包含了应用的依赖,以及spring boot相关的类。下面称之为fat jar。先来查看spring boot打好的包的目录结构(不重要的省略掉): META-INF MANIFEST.MF perties com example SpringBootDemoAp

4、plication.class lib aopalliance-1.0.jar spring-beans-4.2.3.RELEASE.jar . org springframework boot loader ExecutableArchiveLauncher.class JarLauncher.class JavaAgentDetector.class LaunchedURLClassLoader.class Launcher.class MainMethodRunner.class . 123456789101112131415161718192021依次来看下这些内容。MANIFEST.

5、MFManifest-Version: 1.0Start-Class: com.example.SpringBootDemoApplicationImplementation-Vendor-Id: com.exampleSpring-Boot-Version: 1.3.0.RELEASECreated-By: Apache Maven 3.3.3Build-Jdk: 1.8.0_60Implementation-Vendor: Pivotal Software, Inc.Main-Class: org.springframework.boot.loader.JarLauncher1234567

6、8可以看到有Main-Class是org.springframework.boot.loader.JarLauncher ,这个是jar启动的Main函数。还有一个Start-Class是com.example.SpringBootDemoApplication,这个是我们应用自己的Main函数。SpringBootApplicationpublic class SpringBootDemoApplication public static void main(String args) SpringApplication.run(SpringBootDemoApplication.class,

7、 args); 1234567com/example 目录这下面放的是应用的.class文件。lib目录这里存放的是应用的Maven依赖的jar包文件。比如spring-beans,spring-mvc等/springframework/boot/loader 目录这下面存放的是Spring boot loader的.class文件。Archive的概念archive即归档文件,这个概念在linux下比较常见通常就是一个tar/zip格式的压缩包jar是zip格式在spring boot里,抽象出了Archive的概念。一个archive可以是一个jar(JarFileArchi

8、ve),也可以是一个文件目录(ExplodedArchive)。可以理解为Spring boot抽象出来的统一访问资源的层。上面的demo-0.0.1-SNAPSHOT.jar 是一个Archive,然后demo-0.0.1-SNAPSHOT.jar里的/lib目录下面的每一个Jar包,也是一个Archive。public abstract class Archive public abstract URL getUrl(); public String getMainClass(); public abstract Collection getEntries(); public abstra

9、ct List getNestedArchives(EntryFilter filter);12345可以看到Archive有一个自己的URL,比如:jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/1还有一个getNestedArchives函数,这个实际返回的是demo-0.0.1-SNAPSHOT.jar/lib下面的jar的Archive列表。它们的URL是:jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/lib/aopalliance-1.0.jarjar:file:/tmp/target/demo

10、-0.0.1-SNAPSHOT.jar!/lib/spring-beans-4.2.3.RELEASE.jar12JarLauncher从MANIFEST.MF可以看到Main函数是JarLauncher,下面来分析它的工作流程。JarLauncher类的继承结构是:class JarLauncher extends ExecutableArchiveLauncherclass ExecutableArchiveLauncher extends Launcher12以demo-0.0.1-SNAPSHOT.jar创建一个Archive:JarLauncher先找到自己所在的jar,即demo-

11、0.0.1-SNAPSHOT.jar的路径,然后创建了一个Archive。下面的代码展示了如何从一个类找到它的加载的位置的技巧: protected final Archive createArchive() throws Exception ProtectionDomain protectionDomain = getClass().getProtectionDomain(); CodeSource codeSource = protectionDomain.getCodeSource(); URI location = (codeSource = null ? null : codeSou

12、rce.getLocation().toURI(); String path = (location = null ? null : location.getSchemeSpecificPart(); if (path = null) throw new IllegalStateException(Unable to determine code source archive); File root = new File(path); if (!root.exists() throw new IllegalStateException( Unable to determine code sou

13、rce archive from + root); return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root); 12345678910111213141516获取lib/下面的jar,并创建一个LaunchedURLClassLoaderJarLauncher创建好Archive之后,通过getNestedArchives函数来获取到demo-0.0.1-SNAPSHOT.jar/lib下面的所有jar文件,并创建为List。注意上面提到,Archive都是有自己的URL的。获取到这些Ar

14、chive的URL之后,也就获得了一个URL数组,用这个来构造一个自定义的ClassLoader:LaunchedURLClassLoader。创建好ClassLoader之后,再从MANIFEST.MF里读取到Start-Class,即com.example.SpringBootDemoApplication,然后创建一个新的线程来启动应用的Main函数。 /* * Launch the application given the archive file and a fully configured classloader. */ protected void launch(String

15、args, String mainClass, ClassLoader classLoader) throws Exception Runnable runner = createMainMethodRunner(mainClass, args, classLoader); Thread runnerThread = new Thread(runner); runnerThread.setContextClassLoader(classLoader); runnerThread.setName(Thread.currentThread().getName(); runnerThread.sta

16、rt(); /* * Create the code MainMethodRunner used to launch the application. */ protected Runnable createMainMethodRunner(String mainClass, String args, ClassLoader classLoader) throws Exception Class runnerClass = classLoader.loadClass(RUNNER_CLASS); Constructor constructor = runnerClass.getConstruc

17、tor(String.class, String.class); return (Runnable) constructor.newInstance(mainClass, args); 12345678910111213141516171819202122LaunchedURLClassLoaderLaunchedURLClassLoader和普通的URLClassLoader的不同之处是,它提供了从Archive里加载.class的能力。结合Archive提供的getEntries函数,就可以获取到Archive里的Resource。当然里面的细节还是很多的,下面再描述。spring boo

18、t应用启动流程总结看到这里,可以总结下Spring Boot应用的启动流程:spring boot应用打包之后,生成一个fat jar,里面包含了应用依赖的jar包,还有Spring boot loader相关的类Fat jar的启动Main函数是JarLauncher,它负责创建一个LaunchedURLClassLoader来加载/lib下面的jar,并以一个新线程启动应用的Main函数。spring boot loader里的细节代码地址: HYPERLINK t _blank JarFile URL的扩展Spring boot能做到以一个fat jar来启动,最重要的一点是它实现了ja

19、r in jar的加载方式。JDK原始的JarFile URL的定义可以参考这里: HYPERLINK t _blank 原始的JarFile URL是这样子的:jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/1jar包里的资源的URL:jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/com/example/SpringBootDemoApplication.class1可以看到对于Jar里的资源,定义以!/来分隔。原始的JarFile URL只支持一个!/。Spring boot扩展了这个协议,让它支持多

20、个!/,就可以表示jar in jar,jar in directory的资源了。比如下面的URL表示demo-0.0.1-SNAPSHOT.jar这个jar里lib目录下面的spring-beans-4.2.3.RELEASE.jar里面的MANIFEST.MF:jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/lib/spring-beans-4.2.3.RELEASE.jar!/META-INF/MANIFEST.MF1自定义URLStreamHandler,扩展JarFile和JarURLConnection在构造一个URL时,可以传递一个Ha

21、ndler,而JDK自带有默认的Handler类,应用可以自己注册Handler来处理自定义的URL。public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException123456参考: HYPERLINK l URL-java.lang.String-java.lang.String-int-java.lang.String- t _blank Spring boot通过注册了一个自定义的Handler类来处理多重jar i

22、n jar的逻辑。这个Handler内部会用SoftReference来缓存所有打开过的JarFile。在处理像下面这样的URL时,会循环处理!/分隔符,从最上层出发,先构造出demo-0.0.1-SNAPSHOT.jar这个JarFile,再构造出spring-beans-4.2.3.RELEASE.jar这个JarFile,然后再构造出指向MANIFEST.MF的JarURLConnection。jar:file:/tmp/target/demo-0.0.1-SNAPSHOT.jar!/lib/spring-beans-4.2.3.RELEASE.jar!/META-INF/MANIFES

23、T.MF1/org.springframework.boot.loader.jar.Handlerpublic class Handler extends URLStreamHandler private static final String SEPARATOR = !/; private static SoftReferenceMap rootFileCache; Override protected URLConnection openConnection(URL url) throws IOException if (this.jarFile != null) return new J

24、arURLConnection(url, this.jarFile); try return new JarURLConnection(url, getRootJarFileFromUrl(url); catch (Exception ex) return openFallbackConnection(url, ex); public JarFile getRootJarFileFromUrl(URL url) throws IOException String spec = url.getFile(); int separatorIndex = spec.indexOf(SEPARATOR)

25、; if (separatorIndex = -1) throw new MalformedURLException(Jar URL does not contain !/ separator); String name = spec.substring(0, separatorIndex); return getRootJarFile(name); 12345678910111213141516171819202122232425ClassLoader如何读取到Resource对于一个ClassLoader,它需要哪些能力?查找资源读取资源对应的API是:public URL findRes

26、ource(String name)public InputStream getResourceAsStream(String name)12上面提到,Spring boot构造LaunchedURLClassLoader时,传递了一个URL数组。数组里是lib目录下面的jar的URL。对于一个URL,JDK或者ClassLoader如何知道怎么读取到里面的内容的?实际上流程是这样子的:LaunchedURLClassLoader.loadClassURL.getContent()URL.openConnection()Handler.openConnection(URL)最终调用的是JarU

27、RLConnection的getInputStream()函数。/org.springframework.boot.loader.jar.JarURLConnection Override public InputStream getInputStream() throws IOException connect(); if (this.jarEntryName.isEmpty() throw new IOException(no entry name specified); return this.jarEntryData.getInputStream(); 123456789从一个URL,

28、到最终读取到URL里的内容,整个过程是比较复杂的,总结下:spring boot注册了一个Handler来处理”jar:”这种协议的URLspring boot扩展了JarFile和JarURLConnection,内部处理jar in jar的情况在处理多重jar in jar的URL时,spring boot会循环处理,并缓存已经加载到的JarFile对于多重jar in jar,实际上是解压到了临时目录来处理,可以参考JarFileArchive里的代码在获取URL的InputStream时,最终获取到的是JarFile里的JarEntryData这里面的细节很多,只列出比较重要的一些点

29、。然后,URLClassLoader是如何getResource的呢?URLClassLoader在构造时,有URL数组参数,它内部会用这个数组来构造一个URLClassPath:URLClassPath ucp = new URLClassPath(urls);1在 URLClassPath 内部会为这些URLS 都构造一个Loader,然后在getResource时,会从这些Loader里一个个去尝试获取。如果获取成功的话,就像下面那样包装为一个Resource。Resource getResource(final String name, boolean check) final URL

30、 url; try url = new URL(base, ParseUtil.encodePath(name, false); catch (MalformedURLException e) throw new IllegalArgumentException(name); final URLConnection uc; try if (check) URLClassPath.check(url); uc = url.openConnection(); InputStream in = uc.getInputStream(); if (uc instanceof JarURLConnecti

31、on) /* Need to remember the jar file so it can be closed * in a hurry. */ JarURLConnection juc = (JarURLConnection)uc; jarfile = JarLoader.checkJar(juc.getJarFile(); catch (Exception e) return null; return new Resource() public String getName() return name; public URL getURL() return url; public URL

32、 getCodeSourceURL() return base; public InputStream getInputStream() throws IOException return uc.getInputStream(); public int getContentLength() throws IOException return uc.getContentLength(); ;123456789101112131415161718192021222324252627282930313233343536从代码里可以看到,实际上是调用了url.openConnection()。这样完整

33、的链条就可以连接起来了。注意,URLClassPath这个类的代码在JDK里没有自带,在这里看到 HYPERLINK l 506 t _blank 在IDE/开放目录启动Spring boot应用在上面只提到在一个fat jar里启动Spring boot应用的过程,下面分析IDE里Spring boot是如何启动的。在IDE里,直接运行的Main函数是应用自己的Main函数:SpringBootApplicationpublic class SpringBootDemoApplication public static void main(String args) SpringApplica

34、tion.run(SpringBootDemoApplication.class, args); 1234567其实在IDE里启动Spring boot应用是最简单的一种情况,因为依赖的Jar都让IDE放到classpath里了,所以Spring boot直接启动就完事了。还有一种情况是在一个开放目录下启动Spring boot启动。所谓的开放目录就是把fat jar解压,然后直接启动应用。java org.springframework.boot.loader.JarLauncher1这时,Spring boot会判断当前是否在一个目录里,如果是的,则构造一个ExplodedArchive(

35、前面在jar里时是JarFileArchive),后面的启动流程类似fat jar的。Embead Tomcat的启动流程判断是否在web环境spring boot在启动时,先通过一个简单的查找Servlet类的方式来判断是不是在web环境:private static final String WEB_ENVIRONMENT_CLASSES = javax.servlet.Servlet, org.springframework.web.context.ConfigurableWebApplicationContext ;private boolean deduceWebEnvironmen

36、t() for (String className : WEB_ENVIRONMENT_CLASSES) if (!ClassUtils.isPresent(className, null) return false; return true;1234567891011如果是的话,则会创建AnnotationConfigEmbeddedWebApplicationContext,否则Spring context就是AnnotationConfigApplicationContext:/org.springframework.boot.SpringApplication protected Co

37、nfigurableApplicationContext createApplicationContext() Class contextClass = this.applicationContextClass; if (contextClass = null) try contextClass = Class.forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS); catch (ClassNotFoundException ex) throw new IllegalStateExcep

38、tion( Unable create a default ApplicationContext, + please specify an ApplicationContextClass, ex); return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass); 1234567891011121314151617获取EmbeddedServletContainerFactory的实现类spring boot通过获取EmbeddedServletContainerFactory来启动对应的web服务器。常用

39、的两个实现类是TomcatEmbeddedServletContainerFactory和JettyEmbeddedServletContainerFactory。启动Tomcat的代码:/TomcatEmbeddedServletContainerFactoryOverridepublic EmbeddedServletContainer getEmbeddedServletContainer( ServletContextInitializer. initializers) Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDir

40、ectory != null ? this.baseDirectory : createTempDir(tomcat); tomcat.setBaseDir(baseDir.getAbsolutePath(); Connector connector = new Connector(tocol); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(fal

41、se); tomcat.getEngine().setBackgroundProcessorDelay(-1); for (Connector additionalConnector : this.additionalTomcatConnectors) tomcat.getService().addConnector(additionalConnector); prepareContext(tomcat.getHost(), initializers); return getTomcatEmbeddedServletContainer(tomcat);123456789101112131415

42、1617181920会为tomcat创建一个临时文件目录,如:/tmp/tomcat.2233614112516545210.8080,做为tomcat的basedir。里面会放tomcat的临时文件,比如work目录。还会初始化Tomcat的一些Servlet,比如比较重要的default/jsp servlet:private void addDefaultServlet(Context context) Wrapper defaultServlet = context.createWrapper(); defaultServlet.setName(default); defaultSer

43、vlet.setServletClass(org.apache.catalina.servlets.DefaultServlet); defaultServlet.addInitParameter(debug, 0); defaultServlet.addInitParameter(listings, false); defaultServlet.setLoadOnStartup(1); / Otherwise the default location of a Spring DispatcherServlet cannot be set defaultServlet.setOverridab

44、le(true); context.addChild(defaultServlet); context.addServletMapping(/, default);private void addJspServlet(Context context) Wrapper jspServlet = context.createWrapper(); jspServlet.setName(jsp); jspServlet.setServletClass(getJspServletClassName(); jspServlet.addInitParameter(fork, false); jspServl

45、et.setLoadOnStartup(3); context.addChild(jspServlet); context.addServletMapping(*.jsp, jsp); context.addServletMapping(*.jspx, jsp);1234567891011121314151617181920212223spring boot的web应用如何访问Resource当spring boot应用被打包为一个fat jar时,是如何访问到web resource的?实际上是通过Archive提供的URL,然后通过Classloader提供的访问classpath res

46、ource的能力来实现的。index.html比如需要配置一个index.html,这个可以直接放在代码里的src/main/resources/static目录下。对于index.html欢迎页,spring boot在初始化时,就会创建一个ViewController来处理:/ResourcePropertiespublic class ResourceProperties implements ResourceLoaderAware private static final String SERVLET_RESOURCE_LOCATIONS = / ; private static fi

47、nal String CLASSPATH_RESOURCE_LOCATIONS = classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/ ;123456789/WebMvcAutoConfigurationAdapter Override public void addViewControllers(ViewControllerRegistry registry) Resource page = this.resourceProperties.getWelcom

48、ePage(); if (page != null) (Adding welcome page: + page); registry.addViewController(/).setViewName(forward:index.html); 123456789template像页面模板文件可以放在src/main/resources/template目录下。但这个实际上是模板的实现类自己处理的。比如ThymeleafProperties类里的:public static final String DEFAULT_PREFIX = classpath:/templates/

49、;1jspjsp页面和template类似。实际上是通过spring mvc内置的JstlView来处理的。可以通过配置spring.view.prefix来设定jsp页面的目录:spring.view.prefix: /WEB-INF/jsp/1spring boot里统一的错误页面的处理对于错误页面,Spring boot也是通过创建一个BasicErrorController来统一处理的。ControllerRequestMapping($server.error.path:$error.path:/error)public class BasicErrorController exte

50、nds AbstractErrorController 1234对应的View是一个简单的HTML提醒: Configuration ConditionalOnProperty(prefix = server.error.whitelabel, name = enabled, matchIfMissing = true) Conditional(ErrorTemplateMissingCondition.class) protected static class WhitelabelErrorViewConfiguration private final SpelView defaultErr

51、orView = new SpelView( Whitelabel Error Page + This application has no explicit mapping for /error, so you are seeing this as a fallback. + $timestamp + There was an unexpected error (type=$error, status=$status). + $message); Bean(name = error) ConditionalOnMissingBean(name = error) public View def

52、aultErrorView() return this.defaultErrorView; 1234567891011121314151617spring boot的这个做法很好,避免了传统的web应用来出错时,默认抛出异常,容易泄密。spring boot应用的maven打包过程先通过maven-shade-plugin生成一个包含依赖的jar,再通过spring-boot-maven-plugin插件把spring boot loader相关的类,还有MANIFEST.MF打包到jar里。spring boot里有颜色日志的实现当在shell里启动spring boot应用时,会发现它的l

53、ogger输出是有颜色的,这个特性很有意思。可以通过这个设置来关闭:spring.output.ansi.enabled=false1原理是通过AnsiOutputApplicationListener ,这个来获取这个配置,然后设置logback在输出时,加了一个 ColorConverter,通过org.springframework.boot.ansi.AnsiOutput ,对一些字段进行了渲染。一些代码小技巧实现ClassLoader时,支持JDK7并行加载可以参考LaunchedURLClassLoader里的LockProviderpublic class LaunchedURL

54、ClassLoader extends URLClassLoader private static LockProvider LOCK_PROVIDER = setupLockProvider(); private static LockProvider setupLockProvider() try ClassLoader.registerAsParallelCapable(); return new Java7LockProvider(); catch (NoSuchMethodError ex) return new LockProvider(); Override protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException synchronized (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论