Spring Boot 初级入门教程(三) —— 代码变更应用热重启
2018年05月24日 22:24:52 SpringBoot ⁄ 共 3479字 暂无评论 ⁄ 被围观 863次

在项目开发的过程中,想必大家知道,类 Eclipse 型 IDE 工具,都有代码自动编译的选项,平时开发都是打上勾的,具体为:Project 下的 Build Automatically。

而开发中有个需求就是,只要自己修改了代码并保存,除了可以自动编译,最好是能自动重启应用,直接测试。否则,还需要启动,甚者是先停止,再启动,特别繁琐。

在 Spring Boot 中,自动重启时可以配置的,可以通过官方提供的插件直接搞定,接下来开始配置。

一、修改 pom 文件。

添加依赖包 spring-boot-devtools,添加插件配置。

原理:

spring-boot-devtools是一个为开发者服务的模块,其中有个很重要的功能就是自动应用代码更改到最新的App上。

深层原理:

在发现代码有更改之后,重新启动应用。它使用了两个ClassLoader,一个加载那些不会改变的类,如第三方jar包;另一个加载会更改的类,称为restart ClassLoader,当代码改变时,原来的rc被丢弃掉,重新创建一个rc。

devtools会监听classpath下的文件变动,并会立即重启程序,由于采用虚拟机机制,该项重启时很快。

devtools也可以实现页面热部署,直接在 application.properties 中配置:spring.thymeleaf.cache=false。注意:不同的模板引擎,配置是不一样的,这里举例只是对 ThymeLeaf 模板引擎的配置。

代码具体如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.menglanglang</groupId>  
  6.     <artifactId>test-springboot</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>test-springboot</name>  
  11.     <url>http://blog.csdn.net/tzhuwb</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>junit</groupId>  
  20.             <artifactId>junit</artifactId>  
  21.             <version>3.8.1</version>  
  22.             <scope>test</scope>  
  23.         </dependency>  
  24.   
  25.         <!-- 该依赖包提供了MVC、AOP等的依赖包 -->  
  26.         <dependency>  
  27.             <groupId>org.springframework.boot</groupId>  
  28.             <artifactId>spring-boot-starter-web</artifactId>  
  29.             <version>1.4.2.RELEASE</version>  
  30.         </dependency>  
  31.   
  32.         <!-- 添加Spring Boot Devtools依赖包 -->  
  33.         <dependency>  
  34.             <groupId>org.springframework.boot</groupId>  
  35.             <artifactId>spring-boot-devtools</artifactId>  
  36.             <version>1.4.2.RELEASE</version>  
  37.             <optional>true</optional>  
  38.         </dependency>  
  39.     </dependencies>  
  40.   
  41.     <build>  
  42.         <plugins>  
  43.             <!-- 这是Spring Boot Devtools Plugin的配置 -->  
  44.             <plugin>  
  45.                 <groupId>org.springframework.boot</groupId>  
  46.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  47.                 <configuration>  
  48.                     <!-- 如果没有fork配置,可能devtools不会起作用,即不会restart -->  
  49.                     <fork>true</fork>  
  50.                 </configuration>  
  51.             </plugin>  
  52.         </plugins>  
  53.     </build>  
  54.   
  55.     <!-- Add Spring repositories -->  
  56.     <!-- (you don't need this if you are using a .RELEASE version) -->  
  57.     <repositories>  
  58.         <repository>  
  59.             <id>spring-snapshots</id>  
  60.             <url>http://repo.spring.io/snapshot</url>  
  61.             <snapshots>  
  62.                 <enabled>true</enabled>  
  63.             </snapshots>  
  64.         </repository>  
  65.         <repository>  
  66.             <id>spring-milestones</id>  
  67.             <url>http://repo.spring.io/milestone</url>  
  68.         </repository>  
  69.     </repositories>  
  70.     <pluginRepositories>  
  71.         <pluginRepository>  
  72.             <id>spring-snapshots</id>  
  73.             <url>http://repo.spring.io/snapshot</url>  
  74.         </pluginRepository>  
  75.         <pluginRepository>  
  76.             <id>spring-milestones</id>  
  77.             <url>http://repo.spring.io/milestone</url>  
  78.         </pluginRepository>  
  79.     </pluginRepositories>  
  80. </project>  

二、测试该功能

启动应用,尝试修改 App.java 中的部分代码,保存,看看高大上的自动重启功能吧^^

原文链接:https://blog.csdn.net/tzhuwb/article/details/80444659

给我留言

留言无头像?