zoukankan      html  css  js  c++  java
  • Spring Boot 实现开发时热部署

    热部署原理:

    它监听到如果有Class文件改动了,就会创建一个新的ClaassLoader进行加载该文件,经过一系列的过程,最终将结果呈现在我们眼前。

    类加载机制:

    Java中的类经过编译器可以把代码编译为存储字节码的Class文件,该Class文件存储了各种信息,最终要加载到虚拟机中运行使用。虚拟机把描述类的数据从Class文件加载到内存中,并对数据进行校验、转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型。

    Spring Boot 实现开发时热部署:

    Spring Boot 实现热部署有如下方式

    使用 Spring Loaded
    使用 spring-boot-devtools

    Spring Loaded
    这种方式是以Maven插件的形式去加载,所以启动时使用通过Maven命令mvn spring-boot:run启动,而通过Application.run方式启动的会无效,因为通过应用程序启动时,已经绕开了Maven插件机制。
    pom集成方式:

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
    <version>1.2.5.RELEASE</version>
    </dependency>
    </dependencies>
    </plugin>
    </plugins>
    </build>

    spring-boot-devtools
    这种方式无论怎么启动应用,都可以达到修改文件后重启应用。
    pom集成:

    <!-- 热部署模块 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
    </dependency>
    集成注意
    如果发现没有热部署效果,则需要检查IDE配置中有没有打开自动编译。
    如果使用Thymeleaf模板引擎,需要把模板默认缓存设置为false

    #禁止thymeleaf缓存(建议:开发环境设置为false,生成环境设置为true)
    spring.thymeleaf.cache=false

    1.针对devtools的可以指定目录或者排除目录来进行热部署
    #添加那个目录的文件需要restart
    spring.devtools.restart.additional-paths=src/main/java
    #排除那个目录的文件不需要restart
    spring.devtools.restart.exclude=static/**,public/**

    设置idea让他实现文件修改自动重启项目
    1.找到idea的Preferences -> Build, Execution, Deployment -> Compiler,勾选Build project automatically
    2.回到idea正常界面,Mac使用快捷键shift+option+command+/,window上的快捷键是Shift+Ctrl+Alt+/,打开Registry,勾选
    compiler.automake.allow.when.app.runningcompiler.automake.allow.when.app.running
    通过以上的设置就可以在不重启服务的情况下加载html,但如果修改java文件,服务在几秒后会自动重启,如果不希望服务重启需要在application.properties或application.yml中添加spring.devtools.reatart.enable=false

  • 相关阅读:
    VIM文本替换命令
    VIM格式化代码(How to format code with VIM)
    字符串匹配的Boyer-Moore算法
    Java中数组的遍历
    UVa10723
    uva242,Stamps and Envelope Size
    UVa1630,Folding
    uva1629,Cake Slicing,记忆化搜索
    uva 10118,记忆化搜索
    uva10003
  • 原文地址:https://www.cnblogs.com/yxfcnbg/p/11547433.html
Copyright © 2011-2022 走看看