zoukankan      html  css  js  c++  java
  • Springboot jar包外指定配置文件及原理

    • 解决方案:
      修改maven的pom.xml文件
    不拷贝资源文件
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>*</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
    </resources>
    
    修改打包方式
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <layout>ZIP</layout>
        </configuration>
    </plugin>
    • 运行
      假设application.properties和application-{profile}.properties都在/tmp/temp/config,jar文件在/tmp/temp
      java -Dloader.path=file:///tmp/temp/config,demo-1.0.jar -jar demo-1.0.jar

    • 原理
      对比jar包中MANIFEST.MF文件在`ZIP配置前后的区别
      配置前:
    Main-Class: org.springframework.boot.loader.JarLauncher
    Start-Class: com.chinaunicom.gateway.GatewayApplication

         配置后:

    Main-Class: org.springframework.boot.loader.PropertiesLauncher
    Start-Class: com.chinaunicom.gateway.GatewayApplication

         发现是类加载器变了,查看org.springframework.boot.loader包下所有加载器实现:
    这里写图片描述

         查看五个类描述:官方文档

    • 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.

    • WarLauncher
      Launcher for WAR based archives. This launcher for standard WAR archives. Supports dependencies in WEB-INF/lib as well as WEB-INF/lib-provided, classes are loaded from WEB-INF/classes.

    • 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.

    • spring-boot-maven-plugin帮助
      对zip格式说明
      对jar和war说明

  • 相关阅读:
    为不喝的朋友准备的!如何委婉地拒绝劝酒
    参加Windows 7 成都软件俱乐部 发布活动
    解决CalendarExtender控件被遮挡的问题,并加上白色背景,以避免多层影响
    VS2008开发环境中容易遇到的3个问题之解决办法
    大开眼界的梦幻PHOTO
    51aspx是流氓!自动修改F5快捷键为打开它的站!
    ASP.NET DropDownList1 数据绑定的效率
    Repeater两层嵌套和三层嵌套repeater1_ItemDataBound绑定
    CuteEditor6.0使用配置心得体会
    谷歌Analytics添加到您的SharePoint 2010网站的2种方法
  • 原文地址:https://www.cnblogs.com/ggzone/p/7512870.html
Copyright © 2011-2022 走看看