SpringBoot 之热部署

默认情况下, 我们修改 class 或者 修改模板文件(templates目录 下面的文件) 等动态资源, 都不会立即自动生效。 在IDEA中, 我通过Ctrl + F9 , 仍然是无效。 当然, 静态资源的修改是可以立即更新的, 但是也是需要Ctrl + F9 编译一次, 另外前端浏览器需要F5刷新一遍。

有没有好的办法呢? 其实 boot 已经 提供了 devtools 这么一个工具。 在 pom.xml 的dependencies标签配置下面的内容后, 就可以了!


org.springframework.boot
spring-boot-devtools

对于 devtools , 我们还可以给设置optional 参数, 据说是可以提供编译速度:

    <optional>true</optional>

有人说,还需要配置一个 fork:

<plugins>  
    <plugin>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-maven-plugin</artifactId>  
        <configuration>  
        <!--fork :  如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->  
          <fork>true</fork>  
        </configuration>  
    </plugin>  
</plugins>  

我测试不是这样的, fork 是 true false 都不要紧。 fork的作用好像不是这个。

这样, 我们就可以随意修改java 源码和 动态资源文件了, 我测试过,新增删除java方法或者java 文件,或者 动态资源文件, 都是可以生效的。

但是, 在IDEA中, 我们需要 按Ctrl + F9 , 有没有办法自动编译呢? IDEA 默认不就是自动编译的吗? 为什么需要 Ctrl + F9? 有没有办法可以不 按Ctrl + F9呢, 当然也是可以的:

IDEA 配置

CTRL + SHIFT + A 查找 勾选 make project automatically 选项
ctrl+shift+alt+/ 查找Registry 勾选 compiler.automake.allow.when.app.running 选项
另外我观察到, 修改java 文件, 按Ctrl + F9 就相当于重启了一遍(观察控制台日志可知)。而模板文件不是这样的,它没有什么日志打印出来, 貌似是仅仅替换了那个文件。

而boot 为DevTools 也是提供了一些配置的。 这些配置可以控制 哪些修改 是否导致restart :

DEVTOOLS (DevToolsProperties)

spring.devtools.livereload.enabled=true # Enable a livereload.com compatible server.
spring.devtools.livereload.port=35729 # Server port.
spring.devtools.restart.additional-exclude= # Additional patterns that should be excluded from triggering a full restart.
spring.devtools.restart.additional-paths= # Additional paths to watch for changes.
spring.devtools.restart.enabled=true # Enable automatic restart.
spring.devtools.restart.exclude=META-INF/maven/,META-INF/resources/,resources/,static/,public/,templates/,/Test.class,/Tests.class,git.properties # Patterns that should be excluded from triggering a full restart.
spring.devtools.restart.poll-interval=1000 # Amount of time (in milliseconds) to wait between polling for classpath changes.
spring.devtools.restart.quiet-period=400 # Amount of quiet time (in milliseconds) required without any classpath changes before a restart is triggered.
spring.devtools.restart.trigger-file= # Name of a specific file that when changed will trigger the restart check. If not specified any classpath file change will trigger the restart.

REMOTE DEVTOOLS (RemoteDevToolsProperties)

spring.devtools.remote.context-path=/.~~spring-boot!~ # Context path used to handle the remote connection.
spring.devtools.remote.debug.enabled=true # Enable remote debug support.
spring.devtools.remote.debug.local-port=8000 # Local remote debug server port.
spring.devtools.remote.proxy.host= # The host of the proxy to use to connect to the remote application.
spring.devtools.remote.proxy.port= # The port of the proxy to use to connect to the remote application.
spring.devtools.remote.restart.enabled=true # Enable remote restart.
spring.devtools.remote.secret= # A shared secret required to establish a connection (required to enable remote support).
spring.devtools.remote.secret-header-name=X-AUTH-TOKEN # HTTP header used to transfer the shared secret.

默认改变 /META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates 等目录文件,会重新重启项目?? ,当然我们编辑静态文件不想重启项目可以配置

spring.devtools.restart.exclude=static/,public/

我们可以通过:

spring.devtools.restart.enabled=false
禁止devtools 进行重启,但是,这样之后, 我们对java 文件的修改就不会生效,这时 Ctrl + F9 也没用, 因为class 文件不会替换。 所以,看来重启还是不可避免的。

参考:

https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/#using-boot-devtools-restart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值