日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot為啥不用配置啟動(dòng)類的實(shí)現(xiàn)

瀏覽:62日期:2023-05-24 15:30:49

前言

在學(xué)習(xí)SparkJava、Vert.x等輕量級(jí)Web框架的時(shí)候,都遇到過打包問題,這兩個(gè)框架打包的時(shí)候都需要添加額外的Maven配置,并指定啟動(dòng)類才能得到可執(zhí)行的JAR包;

而springboot項(xiàng)目,似乎都不需要額外的配置,直接package就可以得到可執(zhí)行的JAR包,這是怎么回事呢?

Vert.x要怎么配?

我們先來看看,Vert.x打包做哪些配置

1)引入maven-shade-plugin插件

2)在插件中指定在package完成時(shí)觸發(fā)shade操作

3)指定啟動(dòng)類

<plugin> <artifactId>maven-shade-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <!--在mvn package完成時(shí)觸發(fā)--> <phase>package</phase> <!--執(zhí)行shade操作--> <goals><goal>shade</goal> </goals> <configuration><transformers> <transformer implementation='org.apache.maven.plugins.shade.resource.ManifestResourceTransformer'> <manifestEntries> <!--指定啟動(dòng)類--> <main-class>com.test.Starter</main-class> </manifestEntries> </transformer></transformers><artifactSet/> </configuration> </execution> </executions></plugin>

效果:

執(zhí)行package操作后,將得到兩個(gè)jar包

①origin-[your project].jar(Maven默認(rèn)打包操作得到的jar包,該包僅包含此項(xiàng)目的類)

②[your project].jar(帶有依賴包,且配置有啟動(dòng)類的可執(zhí)行JAR包)

Spring Boot又是怎么做的

不用添加插件?=> 初始化時(shí)默認(rèn)就有

Spring Boot 初始化得到的項(xiàng)目中,默認(rèn)帶有spring-boot-maven-plugin的Maven配置

SpringBoot打包的基本原理與前面的Vertx配置相同,都是使用maven-shade-plugin(spring-boot-maven-plugin底層使用maven-shade-plugin),在package完成之后,加入依賴的包,并指定啟動(dòng)類。

SpringBoot是在package時(shí),觸發(fā)repackage,將原打包結(jié)果重命名為[your project].jar.original,并得到帶有依賴包和配置好啟動(dòng)類的[your project].jar

不用指定啟動(dòng)類?=> 默認(rèn)掃描得到啟動(dòng)類spring-boot-maven-plugin會(huì)掃描項(xiàng)目,并以帶有@SpringBootApplication注解和main方法的類作為啟動(dòng)類。

默認(rèn)情況下,SpringBoot項(xiàng)目默認(rèn)啟動(dòng)類寫死JarLauncher,該類的main方法再調(diào)用掃描得到的實(shí)際啟動(dòng)類(XXXApplication)的main方法

源碼查看我們從maven repository下載一個(gè)spring-boot-maven-plugin的源碼進(jìn)行查看,查看RepackageMojo.java。

從@Mojo注解中,我們可以知道,該Mojo綁定在PACKAGE(注解中的defaultPhase=LifecyclePhase.PACKAGE),即在package完成后觸發(fā)

/** * Repackages existing JAR and WAR archives so that they can be executed from the command * line using {@literal java -jar}. With <code>layout=NONE</code> can also be used simply * to package a JAR with nested dependencies (and no main class, so not executable). * * @author Phillip Webb * @author Dave Syer * @author Stephane Nicoll * @author Björn Lindström * @since 1.0.0 */@Mojo(name = 'repackage', defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)public class RepackageMojo extends AbstractDependencyFilterMojo { //...}

我們可以看到,該Mojo中可以指定一個(gè)mainclass作為啟動(dòng)類,但是如果沒有指定的時(shí)候,它是如何處理的呢?

/*** The name of the main class. If not specified the first compiled class found that* contains a ’main’ method will be used.* @since 1.0.0*/@Parameterprivate String mainClass;

我們跟蹤這個(gè)mainClass,發(fā)現(xiàn)在此類中,沒有對(duì)這個(gè)mainClass進(jìn)行賦值的操作,只用來構(gòu)造一個(gè)Repackager(也就是說在該Maven插件沒有配置mainClass的時(shí)候,傳給Repackager的就是一個(gè)null),我們觀察到這個(gè)Repackager就是該Mojo執(zhí)行

@Overridepublic void execute() throws MojoExecutionException, MojoFailureException { if (this.project.getPackaging().equals('pom')) { getLog().debug('repackage goal could not be applied to pom project.'); return; } if (this.skip) { getLog().debug('skipping repackaging as per configuration.'); return; } repackage();}private void repackage() throws MojoExecutionException { Artifact source = getSourceArtifact(); File target = getTargetFile(); Repackager repackager = getRepackager(source.getFile()); Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), getFilters(getAdditionalFilters())); Libraries libraries = new ArtifactsLibraries(artifacts, this.requiresUnpack, getLog()); try { LaunchScript launchScript = getLaunchScript(); repackager.repackage(target, libraries, launchScript); //執(zhí)行repackage操作 } catch (IOException ex) { throw new MojoExecutionException(ex.getMessage(), ex); } updateArtifact(source, target, repackager.getBackupFile());}private Repackager getRepackager(File source) { Repackager repackager = new Repackager(source, this.layoutFactory); repackager.addMainClassTimeoutWarningListener(new LoggingMainClassTimeoutWarningListener()); repackager.setMainClass(this.mainClass); //將插件配置的mainClass注入,默認(rèn)就是null if (this.layout != null) { getLog().info('Layout: ' + this.layout); repackager.setLayout(this.layout.layout()); } return repackager;}

由上可知,mainClass的最終確定,應(yīng)該在Repackager的中完成,我繼續(xù)跟蹤該代碼(Repackager來自spring-boot-maven-plugin下引入的spring-boot-loader-tools),打開Repackager的代碼。我們觀察到Repackager的setMainClass并沒有做額外的操作,只是將傳入的參數(shù)set進(jìn)來,但是從注釋中可以得知,其在使用時(shí)如果為空,則會(huì)搜索合適的類作為MainClass

/** * Utility class that can be used to repackage an archive so that it can be executed using * ’{@literal java -jar}’. * * @author Phillip Webb * @author Andy Wilkinson * @author Stephane Nicoll * @since 1.0.0 */public class Repackager { //... /** * Sets the main class that should be run. If not specified the value from the * MANIFEST will be used, or if no manifest entry is found the archive will be * searched for a suitable class. * @param mainClass the main class name */ public void setMainClass(String mainClass) { this.mainClass = mainClass; } //...}

我們就從上面調(diào)用repackage方法開始看

/** * Repackage to the given destination so that it can be launched using ’ * {@literal java -jar}’. * @param destination the destination file (may be the same as the source) * @param libraries the libraries required to run the archive * @param launchScript an optional launch script prepended to the front of the jar * @throws IOException if the file cannot be repackaged * @since 1.3.0 */public void repackage(File destination, Libraries libraries, LaunchScript launchScript) throws IOException { if (destination == null || destination.isDirectory()) { throw new IllegalArgumentException('Invalid destination'); } if (libraries == null) { throw new IllegalArgumentException('Libraries must not be null'); } if (this.layout == null) { this.layout = getLayoutFactory().getLayout(this.source); } destination = destination.getAbsoluteFile(); File workingSource = this.source; if (alreadyRepackaged() && this.source.equals(destination)) { return; } if (this.source.equals(destination)) { workingSource = getBackupFile(); workingSource.delete(); renameFile(this.source, workingSource); } destination.delete(); try { try (JarFile jarFileSource = new JarFile(workingSource)) { repackage(jarFileSource, destination, libraries, launchScript); //這里往下查看 } } finally { if (!this.backupSource && !this.source.equals(workingSource)) { deleteFile(workingSource); } }}private void repackage(JarFile sourceJar, File destination, Libraries libraries, LaunchScript launchScript) throws IOException { WritableLibraries writeableLibraries = new WritableLibraries(libraries); try (JarWriter writer = new JarWriter(destination, launchScript)) { writer.writeManifest(buildManifest(sourceJar)); //注意這里有一個(gè)buildManifest writeLoaderClasses(writer); if (this.layout instanceof RepackagingLayout) { writer.writeEntries(sourceJar, new RenamingEntryTransformer(((RepackagingLayout) this.layout).getRepackagedClassesLocation()), writeableLibraries); } else { writer.writeEntries(sourceJar, writeableLibraries); } writeableLibraries.write(writer); }}private Manifest buildManifest(JarFile source) throws IOException { Manifest manifest = source.getManifest(); if (manifest == null) { manifest = new Manifest(); manifest.getMainAttributes().putValue('Manifest-Version', '1.0'); } manifest = new Manifest(manifest); String startClass = this.mainClass; //mainClass if (startClass == null) { startClass = manifest.getMainAttributes().getValue(MAIN_CLASS_ATTRIBUTE); //先嘗試從mainfest中拿,這個(gè)暫時(shí)不清楚數(shù)據(jù)來源 } if (startClass == null) { startClass = findMainMethodWithTimeoutWarning(source); //這里觸發(fā)搜索mainClass } String launcherClassName = this.layout.getLauncherClassName(); if (launcherClassName != null) { manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName); if (startClass == null) { throw new IllegalStateException('Unable to find main class'); } manifest.getMainAttributes().putValue(START_CLASS_ATTRIBUTE, startClass); } else if (startClass != null) { manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, startClass); } String bootVersion = getClass().getPackage().getImplementationVersion(); manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion); manifest.getMainAttributes().putValue(BOOT_CLASSES_ATTRIBUTE, (this.layout instanceof RepackagingLayout) ? ((RepackagingLayout) this.layout).getRepackagedClassesLocation() : this.layout.getClassesLocation()); String lib = this.layout.getLibraryDestination('', LibraryScope.COMPILE); if (StringUtils.hasLength(lib)) { manifest.getMainAttributes().putValue(BOOT_LIB_ATTRIBUTE, lib); } return manifest;}private String findMainMethodWithTimeoutWarning(JarFile source) throws IOException { long startTime = System.currentTimeMillis(); String mainMethod = findMainMethod(source); //這里往下看 long duration = System.currentTimeMillis() - startTime; if (duration > FIND_WARNING_TIMEOUT) { for (MainClassTimeoutWarningListener listener : this.mainClassTimeoutListeners) { listener.handleTimeoutWarning(duration, mainMethod); } } return mainMethod;}protected String findMainMethod(JarFile source) throws IOException { return MainClassFinder.findSingleMainClass(source, this.layout.getClassesLocation(), SPRING_BOOT_APPLICATION_CLASS_NAME); //在指定Jar文件中查找MainClass}private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = 'org.springframework.boot.autoconfigure.SpringBootApplication';/** * Find a single main class in a given jar file. A main class annotated with an * annotation with the given {@code annotationName} will be preferred over a main * class with no such annotation. * @param jarFile the jar file to search * @param classesLocation the location within the jar containing classes * @param annotationName the name of the annotation that may be present on the main * class * @return the main class or {@code null} * @throws IOException if the jar file cannot be read */public static String findSingleMainClass(JarFile jarFile, String classesLocation, String annotationName) throws IOException { SingleMainClassCallback callback = new SingleMainClassCallback(annotationName); MainClassFinder.doWithMainClasses(jarFile, classesLocation, callback); return callback.getMainClassName();}

從最后幾步中,我們可以知道,查找的mainClass是一個(gè)帶有@SpringBootApplication注解的類。不用說明,該類肯定是帶有main方法,如果你想進(jìn)一步確認(rèn),則可以繼續(xù)查看MainClassFinder的代碼(來自spring-boot-loader-tools)。

//...private static final Type MAIN_METHOD_TYPE = Type.getMethodType(Type.VOID_TYPE, STRING_ARRAY_TYPE);private static final String MAIN_METHOD_NAME = 'main';private static class ClassDescriptor extends ClassVisitor { private final Set<String> annotationNames = new LinkedHashSet<>(); private boolean mainMethodFound; ClassDescriptor() { super(SpringAsmInfo.ASM_VERSION); } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { this.annotationNames.add(Type.getType(desc).getClassName()); return null; } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { //如果訪問方式是public static 且 方法名為 main 且 返回值為 void,則認(rèn)定該類含有main方法 if (isAccess(access, Opcodes.ACC_PUBLIC, Opcodes.ACC_STATIC) && MAIN_METHOD_NAME.equals(name) && MAIN_METHOD_TYPE.getDescriptor().equals(desc)) { this.mainMethodFound = true; } return null; } private boolean isAccess(int access, int... requiredOpsCodes) { for (int requiredOpsCode : requiredOpsCodes) { if ((access & requiredOpsCode) == 0) {return false; } } return true; } boolean isMainMethodFound() { return this.mainMethodFound; } Set<String> getAnnotationNames() { return this.annotationNames; }}//...

到此這篇關(guān)于SpringBoot為啥不用配置啟動(dòng)類的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 啟動(dòng)類內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
91久久精品无嫩草影院| 国产精品毛片在线看| 综合干狼人综合首页| 视频一区欧美精品| 婷婷综合网站| 另类国产ts人妖高潮视频| 蜜臀a∨国产成人精品| 日韩欧美久久| 91精品视频一区二区| 91成人小视频| 国产精品嫩模av在线| 久久久精品区| 国产不卡一区| 色老板在线视频一区二区| 国产 日韩 欧美一区| 欧美精品一卡| 青草综合视频| 精品国产18久久久久久二百| 精精国产xxxx视频在线野外| 亚洲91视频| 在线精品一区二区| 欧美日一区二区在线观看| 国产精品videossex久久发布| 老牛国内精品亚洲成av人片 | 美女久久久久| 在线国产日韩| 美女久久久久久| 日韩欧美一区二区三区在线观看| 精品在线播放| 亚洲精品激情| 久久天堂影院| 亚洲午夜一级| 18国产精品| 精品视频国内| 99热免费精品| 国产精品成人自拍| 亚洲福利专区| 亚洲图片久久| 久久av免费| 久久精品中文| 日韩美女国产精品| 免费福利视频一区二区三区| 欧美在线亚洲综合一区| 97成人在线| 天堂日韩电影| 日韩va亚洲va欧美va久久| 国内精品亚洲| 国产精品美女久久久浪潮软件| 日韩1区2区日韩1区2区| www.九色在线| 婷婷亚洲精品| 日韩av首页| 亚洲人成高清| 福利片在线一区二区| 亚洲黄色在线| 国产中文欧美日韩在线| 丝袜亚洲精品中文字幕一区| 欧美aa在线视频| 亚洲免费成人| 精品国产欧美| 婷婷综合成人| 久久九九国产| 国产精品s色| 好看的亚洲午夜视频在线| 欧美国产另类| 欧美专区一区二区三区| 国产美女高潮在线| 欧美私人啪啪vps| 国产亚洲激情| 日韩欧美字幕| 国产精品亚洲欧美一级在线| 在线亚洲精品| 蜜桃成人精品| 国产精品qvod| 中文字幕日韩高清在线| 国产精品字幕| 欧美黄页在线免费观看| 男人的天堂久久精品| 精品久久久久中文字幕小说| 天堂成人免费av电影一区 | 久久99国产精品视频| 免费在线观看视频一区| 国产高清不卡| 美女视频免费精品| 青草国产精品| 首页国产欧美久久| 激情综合自拍| 超碰99在线| 欧美精品成人| 中文字幕一区二区三区日韩精品| 久久久9色精品国产一区二区三区| 欧美国产三级| 亚洲美女91| 久久xxxx精品视频| 久久中文视频| 亚洲精品国产嫩草在线观看| 精品国产欧美日韩一区二区三区| 婷婷五月色综合香五月| 久久福利一区| 欧美日韩视频| 亚州av乱码久久精品蜜桃| 日韩在线看片| 国产高潮在线| 风间由美中文字幕在线看视频国产欧美| 视频一区免费在线观看| 亚洲国产成人精品女人| 999国产精品视频| 日韩欧美少妇| 亚洲国产成人二区| 国产精品成久久久久| 久久久久伊人| 麻豆成人91精品二区三区| 国产欧美午夜| 国产美女撒尿一区二区| 91大神在线观看线路一区| 亚洲精品乱码日韩| 免费美女久久99| 国产一区导航| 日韩午夜一区| 亚洲福利精品| 欧美二区视频| 婷婷精品进入| 中文在线不卡| 亚洲美洲欧洲综合国产一区| 欧美高清一区| 在线综合欧美| 日本欧美在线看| 中文字幕中文字幕精品| 亚洲精品黄色| 国产午夜久久av| 国产精品九九| 国产一区二区三区视频在线| av综合电影网站| 欧美69视频| 国产精品美女久久久浪潮软件| 久久av在线| 91精品一区| 麻豆精品一区二区综合av| 国产在线一区不卡| 日韩精品看片| 91久久亚洲| 日本一区二区三区中文字幕| 欧美日韩伊人| 国产一区精品福利| 亚洲91久久| 喷白浆一区二区| 国产日韩欧美| 国产精品久久久久久久久久10秀| 日韩久久精品网| 女同性一区二区三区人了人一| 水蜜桃久久夜色精品一区的特点| 青草综合视频| 亚洲天堂资源| 美女精品一区| 麻豆精品久久久| 伊人久久大香线蕉av不卡| 一区二区精彩视频| 日韩精品视频在线看| 国产精品115| 婷婷成人在线| 色8久久久久| 国产精品毛片久久| 老鸭窝亚洲一区二区三区| 国产精品久久免费视频| 亚洲深夜视频| 免费日韩视频| 久久这里只有| 欧美另类综合| 国产精品一区高清| 久久亚洲专区| 日韩亚洲精品在线观看| 国产成人精品福利| 国产日韩综合| 美女视频黄免费的久久| 天堂网在线观看国产精品| 亚洲ww精品| 福利一区二区| 亚洲精品伊人| 国产在线观看www| 免费在线观看一区二区三区| 久久免费影院| 老色鬼久久亚洲一区二区| 精品网站999| 免费在线欧美视频| 97精品国产| 天堂va在线高清一区| 天堂√8在线中文| 日韩精品欧美成人高清一区二区| 伊人网在线播放| 日韩精品欧美成人高清一区二区| 中文字幕色婷婷在线视频| 日本亚洲三级在线| 久久精品官网| 久久99免费视频| 一区在线视频观看| 精品久久网站| 亚洲狼人精品一区二区三区| 高清不卡亚洲| 久久黄色影视| 亚洲欧美日本国产专区一区|