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

您的位置:首頁技術文章
文章詳情頁

SpringBoot為啥不用配置啟動類的實現

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

前言

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

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

Vert.x要怎么配?

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

1)引入maven-shade-plugin插件

2)在插件中指定在package完成時觸發shade操作

3)指定啟動類

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

效果:

執行package操作后,將得到兩個jar包

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

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

Spring Boot又是怎么做的

不用添加插件?=> 初始化時默認就有

Spring Boot 初始化得到的項目中,默認帶有spring-boot-maven-plugin的Maven配置

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

SpringBoot是在package時,觸發repackage,將原打包結果重命名為[your project].jar.original,并得到帶有依賴包和配置好啟動類的[your project].jar

不用指定啟動類?=> 默認掃描得到啟動類spring-boot-maven-plugin會掃描項目,并以帶有@SpringBootApplication注解和main方法的類作為啟動類。

默認情況下,SpringBoot項目默認啟動類寫死JarLauncher,該類的main方法再調用掃描得到的實際啟動類(XXXApplication)的main方法

源碼查看我們從maven repository下載一個spring-boot-maven-plugin的源碼進行查看,查看RepackageMojo.java。

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

/** * 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中可以指定一個mainclass作為啟動類,但是如果沒有指定的時候,它是如何處理的呢?

/*** 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;

我們跟蹤這個mainClass,發現在此類中,沒有對這個mainClass進行賦值的操作,只用來構造一個Repackager(也就是說在該Maven插件沒有配置mainClass的時候,傳給Repackager的就是一個null),我們觀察到這個Repackager就是該Mojo執行

@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); //執行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注入,默認就是null if (this.layout != null) { getLog().info('Layout: ' + this.layout); repackager.setLayout(this.layout.layout()); } return repackager;}

由上可知,mainClass的最終確定,應該在Repackager的中完成,我繼續跟蹤該代碼(Repackager來自spring-boot-maven-plugin下引入的spring-boot-loader-tools),打開Repackager的代碼。我們觀察到Repackager的setMainClass并沒有做額外的操作,只是將傳入的參數set進來,但是從注釋中可以得知,其在使用時如果為空,則會搜索合適的類作為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; } //...}

我們就從上面調用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)); //注意這里有一個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中拿,這個暫時不清楚數據來源 } if (startClass == null) { startClass = findMainMethodWithTimeoutWarning(source); //這里觸發搜索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是一個帶有@SpringBootApplication注解的類。不用說明,該類肯定是帶有main方法,如果你想進一步確認,則可以繼續查看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,則認定該類含有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; }}//...

到此這篇關于SpringBoot為啥不用配置啟動類的實現的文章就介紹到這了,更多相關SpringBoot 啟動類內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
久久av电影| 国产福利91精品一区二区| 99精品网站| 久久的色偷偷| 久久中文字幕一区二区三区| 久久av导航| 国产一区二区三区网| 国产精品福利在线观看播放| 高潮一区二区| 99久久精品国产亚洲精品| 国产一区日韩欧美| 亚洲精品va| 亚洲三级在线| 欧美一级网址| 卡一卡二国产精品| 日韩在线高清| 红桃视频国产精品| 亚洲一二av| 色综合视频一区二区三区日韩| 亚洲影院天堂中文av色| 欧美综合精品| 精品三级在线观看视频| 日韩国产激情| 亚洲女同中文字幕| 亚洲免费毛片| 国产精品成人3p一区二区三区| 精品成人18| 欧美福利专区| 亚洲不卡视频| 精品五月天堂| 在线国产一区| 久久国产尿小便嘘嘘| 电影91久久久| 成人av二区| 欧美天堂在线| 国产欧美一区二区三区精品酒店 | 蜜桃免费网站一区二区三区| 日韩一区二区三区精品| 欧美激情久久久久久久久久久| 国产精品毛片久久| 国产精品99一区二区| 日韩国产欧美在线播放| 国产精品99久久精品| 99国产精品私拍| 国产日韩在线观看视频| 韩国精品主播一区二区在线观看| 视频一区二区国产| 国产精品欧美大片| 欧美精品一区二区久久| 日韩精品成人| 国产亚洲一区二区手机在线观看 | 日韩高清在线观看一区二区| 精品中文在线| 久久国产99| 精品黄色一级片| 亚洲国产不卡| 欧美激情网址| 鲁大师成人一区二区三区| 欧美精品97| 国产亚洲欧洲| 国产高潮在线| 日韩高清在线不卡| 一区二区自拍| 精品视频97| 日韩精品视频在线看| 欧洲亚洲一区二区三区| 久久国产精品色av免费看| 国产综合亚洲精品一区二| 国产欧美高清| 亚洲免费高清| 精品午夜视频| 日韩有码av| 好吊日精品视频| 国产精品原创| 久久丁香四色| 日本亚洲欧洲无免费码在线| 亚洲网站视频| 麻豆一区在线| 亚洲精品无播放器在线播放| 国产一区二区三区自拍| 国精品产品一区| 日韩精品一级| 日韩一区二区免费看| 国产成人精品亚洲日本在线观看| 国产精品一站二站| 亚洲一区久久| 欧美在线资源| 久久激情一区| 日韩中文字幕高清在线观看| 国产美女久久| 日本中文字幕视频一区| 亚洲欧美日韩高清在线| 青青青免费在线视频| 国产精品亚洲人成在99www| 久久亚洲欧洲| 午夜久久免费观看| 日韩欧美一区免费| 国产suv精品一区二区四区视频| 日本va欧美va欧美va精品| 欧美中文日韩| 久久精品成人| 成人羞羞在线观看网站| 国产一区一一区高清不卡| 国产精品免费99久久久| 欧美日韩亚洲一区三区| 日韩精品a在线观看91| 一区二区三区四区日韩| 国产一区91| 日韩视频不卡| 欧美影院三区| 精品一区毛片| 欧美特黄一级| 亚洲精品电影| 天堂资源在线亚洲| 91精品婷婷色在线观看| 捆绑调教日本一区二区三区| 国产成人久久精品麻豆二区 | 青青久久av| 福利视频一区| 国产不卡一区| 国产精品久久久久久久久久妞妞 | 久久亚洲一区| 亚洲视频播放| 欧美日一区二区在线观看| 国产综合色区在线观看| 欧美激情国产在线| 91综合视频| 精品捆绑调教一区二区三区| 成人自拍av| 久久国产小视频| 好看的av在线不卡观看| 久久福利毛片| 亚洲三级视频| 久久精品99久久久| 国产高清精品二区| 成人一区而且| 99精品视频在线| 午夜av成人| 亚洲午夜在线| 国产精品三上| 日韩二区三区四区| 国产精品亚洲欧美日韩一区在线| 精品视频在线观看网站| 91av亚洲| 婷婷综合在线| 日韩精品欧美大片| 国产精品igao视频网网址不卡日韩 | 另类小说一区二区三区| 丰满少妇一区| 香蕉久久精品| 首页欧美精品中文字幕| 91精品在线免费视频| 麻豆国产精品一区二区三区| 精品国模一区二区三区| 红桃视频亚洲| 欧美一区自拍| 黄色在线观看www| 狠狠干成人综合网| 久久狠狠久久| 国产精品字幕| 日韩欧美中文字幕在线视频| 麻豆精品国产91久久久久久| 日韩电影免费在线观看| 亚洲精品小说| 国产精品亚洲综合色区韩国| 91看片一区| 亚洲欧洲国产精品一区| 麻豆视频一区二区| 怡红院精品视频在线观看极品| 日韩精品福利一区二区三区| 欧美激情另类| 蜜桃视频在线观看一区二区| 国产精品3区| 五月天久久久| 国产精品极品在线观看| 99久久久久| 久久国际精品| 久久精品电影| 日韩高清欧美激情| 成人亚洲欧美| 亚久久调教视频| 日本高清不卡一区二区三区视频| 亚洲91在线| 日本欧美不卡| 国产精品一区二区三区美女| 欧美搞黄网站| 久久久国产精品网站| 久久午夜精品| 成人日韩av| 亚洲精品进入| 日本久久成人网| 国产精品对白久久久久粗| 亚洲国内精品| 精品视频久久| 亚州av日韩av| 婷婷成人基地| 国产一区二区亚洲| 日韩超碰人人爽人人做人人添| 九九综合九九| 成人午夜在线|