springboot的类加载器(org.springframework.boot.loader)过程详解
类加载器的分类。
试验:使用maven打包
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>启动类的完整路径</mainClass> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> <layout>ZIP</layout> <includes> <include> <groupId>nothing</groupId> <artifactId>nothing</artifactId> </include> </includes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
各个配置项的作用自行百度~
这样生成的jar包,用压缩工具打开以后是这样
在META-INF下打开MANIFEST.MF文件看看(记事本就可以打开)
可以看到这里的类加载器是:PropertiesLauncher,文档百度翻译如下:
Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible and more amenable to creating well-behaved OS-level services than a model based on executable jars.
Looks in various places for a properties file to extract loader settings, defaulting to application.properties either on the current classpath or in the current working directory. The name of the properties file can be changed by setting a System property loader.config.name (e.g. -Dloader.config.name=foo will look for foo.properties. If that file doesn't exist then tries loader.config.location (with allowed prefixes classpath: and file: or any valid URL). Once that file is located turns it into Properties and extracts optional values (which can also be provided overridden as System properties in case the file doesn't exist):
loader.path: a comma-separated list of directories (containing file resources and/or nested archives in .jar or .zip or archives) or archives to append to the classpath. BOOT-INF/classes,BOOT-INF/lib in the application archive are always used
loader.main: the main method to delegate execution to once the class loader is set up. No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.
启动程序,用于通过属性文件使用用户配置的类路径和主类进行归档。与基于可执行jar的模型相比,这种模型通常更灵活,更易于创建性能良好的OS级服务。
在不同位置查找属性文件以提取加载程序设置,默认为应用程序.属性在当前类路径或当前工作目录中。属性文件的名称可以通过设置系统属性来更改加载程序.config.name(例如-Dloader.config.name=foo会寻找食品属性. 如果该文件不存在,则尝试loader.config.location(使用允许的前缀classpath:和file:或任何有效的URL)。找到该文件后,将其转换为属性并提取可选值(如果该文件不存在,也可以将其作为系统属性进行覆盖):
加载程序.path:以逗号分隔的目录列表(包含文件资源和/或.jar或.zip中的嵌套存档文件)或要附加到类路径的存档文件。总是使用应用程序档案中的BOOT-INF/classes、BOOT-INF/lib
装载机.main:设置类装入器后将执行委托给的主方法。没有默认值,但将返回到在清单.MF,如果有${装载机.home}/中导。
这种也是配置:使用jar包外部的配置文件的启动方式的方法。在linux我们的启动脚本要使用对应的类加载器来启动。
如果你配置成其他的( <layout>ZIP</layout>
此配置项去掉,main-class就会变成JarLauncher)比如:JarLauncher
Launcher for JAR based archives. This launcher assumes that dependency jars are included inside a /BOOT-INF/lib directory and that application classes are included inside a /BOOT-INF/classes directory.
基于JAR的档案的启动程序。这个启动程序假设依赖项jar包含在/BOOT-INF/lib目录中,应用程序类包含在/BOOT-INF/classes目录中。
就会有如下的错误:项目启动失败。
springboot项目启动,调用的是相应的类加载器的main方法,而不是我们自己编写的SpringApplication
Spring Boot Loader提供了一套标准用于执行SpringBoot打包出来的jar,这套标准就是我们的类加载器
下面是一个例子,图中有构造方法,和类方法。
友情链接
到此这篇关于springboot的类加载器(org.springframework.boot.loader)的文章就介绍到这了,更多相关springboot类加载器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Maven项目部署到Jboss出现Failed to create a new SAX parser
这篇文章主要为大家详细介绍了Maven项目部署到Jboss出现Failed to create a new SAX parser的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-11-11
最新评论