美文网首页
Springboot jar动态指定启动类

Springboot jar动态指定启动类

作者: 兴厚 | 来源:发表于2019-07-15 18:33 被阅读0次

    需求: springboot jar 包java -jar 方式启动,动态指定启动类

    知识点

    1.  spring-boot-maven-plugin maven插件配置是可以指定 layout 属性的,不同的layout代表不同的spring launcher, 官方文档描述如下:
       <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
           <version>2.1.6.RELEASE</version>
           <configuration>
               <mainClass>${start-class}</mainClass>
               <layout>ZIP</layout>
           </configuration>
           <executions>
               <execution>
                   <goals>
                       <goal>repackage</goal>
                   </goals>
               </execution>
           </executions>
       </plugin>
       The layout property defaults to a guess based on the archive type (jar or war). The following layouts are available:
       
       JAR: regular executable JAR layout.
       WAR: executable WAR layout. provided dependencies are placed in 
       WEB-INF/lib-provided to avoid any clash when the war is deployed in a servlet container.
       ZIP (alias to DIR): similar to the JAR layout using PropertiesLauncher.
       NONE: Bundle all dependencies and project resources. Does not bundle a bootstrap loader.
       
       解释一下:
       JAR: 生成的MAINFEST.MF文件里的Main-class 为 JarLauncher
       
       WAR:会把依赖的项目放置在 WEB-INF/lib-provided 目录下去避免用户将war包单独
       
       部署在其他servlet容器下,Main-class 没看,有兴趣自己看一下生成的MAINFEST.MF文件
       
       ZIP: 重点,他会把默认的jar打包方式的Launcher替换为PropertiesLauncher实现类,
       
       查阅官方文档可知,这个Launcher可以动态指定Start-class通过配置文件的方式。
       
       接下来会重点分析
       
       NONE: 不使用spring的launcher启动jar包,只会把项目的依赖打进jar包里。
      
    2. lauout 为 zip 的打包方式运行机制:

    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 loader.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.
    
    解释一下: 就是说如果通过java -jar运行这个jar包,首先这个PropertiesLauncher会在classpath下或者当前
    
    工作目录(执行 java -jar 命令的目录)下找寻
    
    一个叫 load.properties 的配置文件,
    
    这个配置文件的名字呢,也可以通过修改系统属性 loader.config.name 来指定,
    
    比如说 -Dloader.config.name=start ,表示会读取 start.properties 配置文件,
    
    也可以通过file://xxx.properties或classpath://xxx.properties 去指定其他路径下的配置文件。
    
    然后呢,找到了配置文件它就会读取里面的这两个属性:
    
    loader.path
    
    loader.name 
    
    好了,loader.path 就可以动态的指定依赖的 jar路径了,建议如果没有这个需求就不要动。
    
    loader.name 就是需要我们添加的了,配置好我们需要使用的启动类就行了
    
    

    应用:


    目录结构图

    命令:

    java -Dloader.config.name=start -jar ./demo.jar   #使用启动的class
    java -Dloader.config.name=stop -jar ./demo.jar   #使用关闭的class
    

    相关文章

      网友评论

          本文标题:Springboot jar动态指定启动类

          本文链接:https://www.haomeiwen.com/subject/gsjokctx.html