zoukankan      html  css  js  c++  java
  • SpringBoot(六):springboot热部署

    在j2ee项目开发中,热部署插件是JRebel。JRebel的使用为开发人员带来了极大的帮助,且挺高了开发便捷。而在SpringBoot开发生态环境中,SpringBoot热部署常用插件是:spring-boot-devtools。下边将会学习devtools的用法。

    注意:springboot不只支持devtools热部署插件,还支持springloaded方式。

    如何使用spring-boot-devtools?

    需要在springboot的pom.xml中引入:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    如何验证spring-boot-devtools起效?

    当启动springboot工程后,继续修改代码,在不重新手动启动springboot工程的情况下,编译修改的代码,验证是否起效。

    1)在springboot工程中,新建一个HelloWordController.java控制类,在控制类中打印“hello-word”

    2)运行springboot入口方法,来启动springboot工程,并在浏览器中浏览地址http://localhost:8080/index,查看打印信息。

    SpringBoot入口函数类:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    
    @ComponentScan("com.dx.controller")
    @EnableAutoConfiguration
    public class ApplicationStarter {
        public static void main(String[] args) {
            SpringApplication.run(ApplicationStarter.class, args);
        }
    }

    运行main函数,成功启动了springboot工程,之后浏览器访问http://localhost:8080后,控制台打印了“hello word”信息。

    3)修改HelloWordController.java:把打印内容修改为“hello-springboot”,并编译“”该控制类,在浏览器中(不重启springboot服务器的情况下)访问http://localhost:8080/index,查看控制台打印信息。此时发现打印信息依然是“hello word”,并未自动编译。

    4)在工程pom.xml追加“spring-boot-devtools”依赖,并把HelloWordController.java打印信息修改回为“hello word”,在浏览器中访问http://localhost:8080/index,此时控制台打印信息为“hello word”。

    5)修改HelloWordController.java:把打印内容修改为“hello-springboot”,并编译“”该控制类,在浏览器中(不重启springboot服务器的情况下)访问http://localhost:8080/index,查看控制台打印信息。此时发现打印信息依然是“hello springboot”,这就说明热部署已经起效。

    spring-boot-devtools原理:

    深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。

    注意:这里边如果是类发生了变化,是发生了较快的重启,而不是不重启springboot服务。

    spring-boot-devtools参数配置:

    1)排除资源与包括资源:

    在applicaiton.properties中添加配置:

    spring.devtools.restart.exclude=static/**,templates/**
    spring.devtools.restart.additional-exclude=public/** (处理默认配置排除之外的)
    spring.devtools.restart.enabled=false (禁止自动重启)

    2)可以使用trigger.file的重启策略:

    在applicaiton.properties中添加配置:

    spring.devtools.restart.triggerFile=trigger.file

    注意:这个文件不要放到default_excludes目录下,一般情况下放到resources文件下。

    IDEA配置实现Ctrl+S保存后自动编译:

    Eclipse默认自动编译,而idea默认手动编译,因此idea需要修改两个参数以达到任意时间自动编译的目的。

    1)IDEA下File-Settings-Compiler-Build Project automatically(注意后面only works not running/debugging,所以要实现热部署就必须打破这个限制,于是乎有了下面设置)

    2)ctrl + shift + alt + /,选择Registry,勾上 Compiler autoMake allow when app running

    测试

    • 修改类–>保存:应用会重启
    • 修改配置文件–>保存:应用会重启
    • 修改页面–>保存:应用不会重启,但会重新加载,页面会刷新(原理是将spring.thymeleaf.cache设为false,参考:Spring Boot配置模板引擎)
  • 相关阅读:
    Install OpenCV 3.0 and Python 2.7+ on OSX
    Install OpenCV 3.0 and Python 2.7+ on Ubuntu
    完美商业计划书全攻略
    No module named zope.interface error的解决
    nginx flv点播服务器搭建
    关于Apple开发者的D-U-N-S Number
    小而美的Promise库——promiz源码浅析
    为什么JavaScript里面0.1+0.2 === 0.3是false
    JavaScript取出字符串中括号里的内容
    js数据结构与算法--递归
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/8760790.html
Copyright © 2011-2022 走看看